$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to print the value of a custom control in the Customizer?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far