最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Need help setting default setting value for radio button in theme customizer

programmeradmin5浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 2

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

发布评论

评论列表(0)

  1. 暂无评论