How to print the value of a custom control in the Customizer?

admin2025-06-04  1

To the Customizer section of my WordPress admin area, I have added a custom setting and custom control. I would like to now print the value of this control. How can I do this? The code used to add the custom setting/control is below (from the WordPress customize_register() page):

function themename_customize_register($wp_customize){
        $wp_customize->add_setting( 'test_setting', array(
            'default'        => 'value_xyz',
            'capability'     => 'edit_theme_options',
            'type'           => 'option',
        ));
        $wp_customize->add_control( 'test_control', array(
            'label'      => __('Text Test', 'themename'),
            'section'    =>  'spacious_slider_number_section1',
            'settings'   => 'test_setting',
        ));
}
add_action('customize_register', 'themename_customize_register');

The text input field displays as expected in the Customizer (screenshot). I would like to now echo the value of this text input field When my page's PHP template is loaded. But when I try to do so, a blank value is returned. The code I used to do so, added to my page's PHP template, is below:

echo get_theme_mod('test_setting');

Furthermore, the type seems to be boolean (instead of string, as I would expect), i.e. gettype(get_theme_mod('test_setting'); returns boolean.

Finally, If I print the value of get_theme_mods(), my custom setting/control does not appear in the array.

To the Customizer section of my WordPress admin area, I have added a custom setting and custom control. I would like to now print the value of this control. How can I do this? The code used to add the custom setting/control is below (from the WordPress customize_register() page):

function themename_customize_register($wp_customize){
        $wp_customize->add_setting( 'test_setting', array(
            'default'        => 'value_xyz',
            'capability'     => 'edit_theme_options',
            'type'           => 'option',
        ));
        $wp_customize->add_control( 'test_control', array(
            'label'      => __('Text Test', 'themename'),
            'section'    =>  'spacious_slider_number_section1',
            'settings'   => 'test_setting',
        ));
}
add_action('customize_register', 'themename_customize_register');

The text input field displays as expected in the Customizer (screenshot). I would like to now echo the value of this text input field When my page's PHP template is loaded. But when I try to do so, a blank value is returned. The code I used to do so, added to my page's PHP template, is below:

echo get_theme_mod('test_setting');

Furthermore, the type seems to be boolean (instead of string, as I would expect), i.e. gettype(get_theme_mod('test_setting'); returns boolean.

Finally, If I print the value of get_theme_mods(), my custom setting/control does not appear in the array.

Share Improve this question asked Jan 2, 2019 at 12:14 cag8fcag8f 1,9973 gold badges21 silver badges31 bronze badges 4
  • Your code is good except in add_setting.You have assigned option for type parameter. It will save separate option for each field. If you use 'type' => 'theme_mod' it will solve your problem. – Tejas Gajjar Commented Jan 2, 2019 at 12:22
  • please try this : $test = get_theme_mod( 'test_setting' ); echo $test; – vikrant zilpe Commented Jan 2, 2019 at 12:40
  • 1 What was mentioned above about using theme_mod instead of option is correct. However, you should also ALWAYS use the 2nd argument in the get_theme_mod function, otherwise it will return false (bool) if the value has not been saved in the customizer. You need to use theme-mods and also define the 2nd argument in get_theme_mod which is the default value that should be used as a fallback when the theme_mod is not saved in the db. – Aristeides Commented Jan 2, 2019 at 19:34
  • @Aristeides Thanks. I had been omitting it, but I now see that it is very helpful with troubleshooting, especially when the eventual control I insert will be a checkbox, which takes Boolean values. – cag8f Commented Jan 5, 2019 at 6:55
Add a comment  | 

1 Answer 1

Reset to default 6

Your code is perfect just need to change 'theme_mod' instead of 'option' it will solve this.

function themename_customize_register($wp_customize){
        $wp_customize->add_setting( 'test_setting', array(
            'default'        => 'value_xyz',
            'capability'     => 'edit_theme_options',
            'type'           => 'theme_mod',
        ));
        $wp_customize->add_control( 'test_control', array(
            'label'      => __('Text Test', 'themename'),
            'section'    =>  'spacious_slider_number_section1',
            'settings'   => 'test_setting',
        ));
}
add_action('customize_register', 'themename_customize_register');

And to retrieve it

get_theme_mod( 'test_setting' ); 

Hope it helps you out.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749051080a315938.html

最新回复(0)