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?
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
andList
and I preferGrid
is default choosing. I should add default value inadd_setting
simple like this'default' => 'grid'
.If not working wordpress customizer default value from add_setting();
You can use it like this-
Here 1st parameter is Setting id and 2nd parameter is Default value
Hope! it will helpful for you.