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

Multi tool use
Multi tool use












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
    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


















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
    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
















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 Jan 2 at 12:14









cag8fcag8f

4261511




4261511












  • 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




















  • 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


















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




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




please try this : $test = get_theme_mod( 'test_setting' ); echo $test;
– vikrant zilpe
Jan 2 at 12:40




1




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






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






@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












1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    Jan 2 at 13:53










  • @Tejas thanks, it worked. Just so I learn more about my problem and the fix, is there official documentation about the 'type' parameter in this case? This page doesn't say much about it: developer.wordpress.org/reference/classes/wp_customize_manager/…
    – cag8f
    Jan 4 at 12:46











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









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.






share|improve this answer





















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    Jan 2 at 13:53










  • @Tejas thanks, it worked. Just so I learn more about my problem and the fix, is there official documentation about the 'type' parameter in this case? This page doesn't say much about it: developer.wordpress.org/reference/classes/wp_customize_manager/…
    – cag8f
    Jan 4 at 12:46
















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.






share|improve this answer





















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    Jan 2 at 13:53










  • @Tejas thanks, it worked. Just so I learn more about my problem and the fix, is there official documentation about the 'type' parameter in this case? This page doesn't say much about it: developer.wordpress.org/reference/classes/wp_customize_manager/…
    – cag8f
    Jan 4 at 12:46














5












5








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.






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 Jan 2 at 12:27









Tejas GajjarTejas Gajjar

1




1












  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    Jan 2 at 13:53










  • @Tejas thanks, it worked. Just so I learn more about my problem and the fix, is there official documentation about the 'type' parameter in this case? This page doesn't say much about it: developer.wordpress.org/reference/classes/wp_customize_manager/…
    – cag8f
    Jan 4 at 12:46


















  • If type is set to option then you can get the value with get_option().
    – Jacob Peattie
    Jan 2 at 13:53










  • @Tejas thanks, it worked. Just so I learn more about my problem and the fix, is there official documentation about the 'type' parameter in this case? This page doesn't say much about it: developer.wordpress.org/reference/classes/wp_customize_manager/…
    – cag8f
    Jan 4 at 12:46
















If type is set to option then you can get the value with get_option().
– Jacob Peattie
Jan 2 at 13:53




If type is set to option then you can get the value with get_option().
– Jacob Peattie
Jan 2 at 13:53












@Tejas thanks, it worked. Just so I learn more about my problem and the fix, is there official documentation about the 'type' parameter in this case? This page doesn't say much about it: developer.wordpress.org/reference/classes/wp_customize_manager/…
– cag8f
Jan 4 at 12:46




@Tejas thanks, it worked. Just so I learn more about my problem and the fix, is there official documentation about the 'type' parameter in this case? This page doesn't say much about it: developer.wordpress.org/reference/classes/wp_customize_manager/…
– cag8f
Jan 4 at 12:46


















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.




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







h5QHTY
YcBeM7I RJHhG s4ns,U Rh3iuSL3mkvGNAQ3,niLmtgPUAwIxFovg a5RkT

Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

張江高科駅