3

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.

  • 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 Jan 2 at 12:22
  • please try this : $test = get_theme_mod( 'test_setting' ); echo $test; – vikrant zilpe Jan 2 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 Jan 2 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 Jan 5 at 6:55
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.