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

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?

2 s
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'),
        ),
    ));

Leave a Comment