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












2














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






















  • 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
    2 hours ago










  • please try this : $test = get_theme_mod( 'test_setting' ); echo $test;
    – vikrant zilpe
    1 hour ago
















2














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






















  • 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
    2 hours ago










  • please try this : $test = get_theme_mod( 'test_setting' ); echo $test;
    – vikrant zilpe
    1 hour ago














2












2








2







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













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.







theme-customizer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









cag8f

4241511




4241511












  • 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
    2 hours ago










  • please try this : $test = get_theme_mod( 'test_setting' ); echo $test;
    – vikrant zilpe
    1 hour ago


















  • 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
    2 hours ago










  • please try this : $test = get_theme_mod( 'test_setting' ); echo $test;
    – vikrant zilpe
    1 hour ago
















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
2 hours ago




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
2 hours ago












please try this : $test = get_theme_mod( 'test_setting' ); echo $test;
– vikrant zilpe
1 hour ago




please try this : $test = get_theme_mod( 'test_setting' ); echo $test;
– vikrant zilpe
1 hour ago










1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer





















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    38 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f324431%2fhow-to-print-the-value-of-a-custom-control-in-the-customizer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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.






share|improve this answer





















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    38 mins ago
















2














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.






share|improve this answer





















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    38 mins ago














2












2








2






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.






share|improve this 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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









Tejas Gajjar

1636




1636












  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    38 mins ago


















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    38 mins ago
















If type is set to option then you can get the value with get_option().
– Jacob Peattie
38 mins ago




If type is set to option then you can get the value with get_option().
– Jacob Peattie
38 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to WordPress Development Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f324431%2fhow-to-print-the-value-of-a-custom-control-in-the-customizer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Understanding the information contained in the Deep Space Network XML data?

Ross-on-Wye

Eastern Orthodox Church