admin管理员组文章数量:1410706
I need help figuring out how the radio button saves it's value in the theme settings.
In my theme I added Radio buttons in the theme customizer. So far they are working great. Although I have one issue. I can't seem to set a default setting for them, so if I install my theme on a new install, or someone else does. Than whatever the radio buttons are for doesn't appear. I am not talking about the default value of the radio button itself, I am talking about the default theme customizers settings value. Here is how I create the radio buttons:
/** Header button options */
$wp_customize->add_setting(
'header_btn_options',
array(
'default' => 'duplicatebtn',
) );
$wp_customize->add_control(
'header_btn_options',
array(
'type' => 'radio',
'priority' => 30,
'label' => 'Header buttons type',
'section' => 'mytheme_header',
'choices' => array(
'duplicatebtn' => 'Use hero settings',
'createcustom' => 'Use custom settings(modify below)',
'hidebtn' => 'Do not display button',
),
)
);
Here is how I set my theme setting defaults. I do this so that when a new install is done the theme looks okay.
function mytheme_get_theme_mods() {
$defaults = array(
'mytheme_header_btn_options' => 'duplicatebtn'
);
return $defaults;
So this is my confusion. Even though I tried setting my default to a choice from the array, it does not work. Although when I save a new value from the radio buttons on the theme customizer it works like that. So it clearly has some value just not "duplicatebtn", or maybe it is and I am doing it wrong. Can anyone please help me figure this out?
I need help figuring out how the radio button saves it's value in the theme settings.
In my theme I added Radio buttons in the theme customizer. So far they are working great. Although I have one issue. I can't seem to set a default setting for them, so if I install my theme on a new install, or someone else does. Than whatever the radio buttons are for doesn't appear. I am not talking about the default value of the radio button itself, I am talking about the default theme customizers settings value. Here is how I create the radio buttons:
/** Header button options */
$wp_customize->add_setting(
'header_btn_options',
array(
'default' => 'duplicatebtn',
) );
$wp_customize->add_control(
'header_btn_options',
array(
'type' => 'radio',
'priority' => 30,
'label' => 'Header buttons type',
'section' => 'mytheme_header',
'choices' => array(
'duplicatebtn' => 'Use hero settings',
'createcustom' => 'Use custom settings(modify below)',
'hidebtn' => 'Do not display button',
),
)
);
Here is how I set my theme setting defaults. I do this so that when a new install is done the theme looks okay.
function mytheme_get_theme_mods() {
$defaults = array(
'mytheme_header_btn_options' => 'duplicatebtn'
);
return $defaults;
So this is my confusion. Even though I tried setting my default to a choice from the array, it does not work. Although when I save a new value from the radio buttons on the theme customizer it works like that. So it clearly has some value just not "duplicatebtn", or maybe it is and I am doing it wrong. Can anyone please help me figure this out?
Share Improve this question asked Nov 16, 2013 at 3:38 user1632018user1632018 4843 gold badges10 silver badges26 bronze badges 3- I also had similar problem on one of my theme for setting defaults. I will keep watching this Q. – Sisir Commented Nov 16, 2013 at 8:13
- Yea, it is a little confusing. I thought was the way it saves the setting, but that can't be true since the switch that changes the content of each radio case works fine in my theme customizer live preview. – user1632018 Commented Nov 16, 2013 at 18:13
- Answered in wordpress.stackexchange/questions/140435/… – Ashiquzzaman Kiron Commented Aug 5, 2016 at 13:35
2 Answers
Reset to default 2It is a long question but it is possible for some developers I would like to give this answer.
Example: I have two choose for my home layout Grid
and List
and I prefer Grid
is default choosing. I should add default value in add_setting
simple like this 'default' => 'grid'
.
$wp_customize->add_setting('yourtheme_home_layout_style',
array(
'sanitize_callback' => 'sanitize_text_field',
'default' => 'grid'
));
$wp_customize->add_control( 'yourtheme_home_layout_style', array(
'section' => 'yourtheme_home_layout_section',
'label' => __( 'Layout Style', 'textdomain' ),
'type' => 'radio',
'priority' => 1,
'choices' => array(
'grid' => __('Grid', 'textdomain'),
'list' => __('List', 'textdomain'),
),
));
If not working wordpress customizer default value from add_setting(); You can use it like this-
<?php echo esc_attr(get_theme_mod('header_btn_options','duplicatebtn'); ?>
Here 1st parameter is Setting id and 2nd parameter is Default value
Hope! it will helpful for you.
本文标签: phpNeed help setting default setting value for radio button in theme customizer
版权声明:本文标题:php - Need help setting default setting value for radio button in theme customizer 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744865545a2629322.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论