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.
1 Answer
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.