I want to create a condition so that by selecting an option from the radio, the settings specific to it will be displayed.
I searched a lot for this topic and couldn’t find anything useful, so I wrote the simple code below.
This code almost works, that is, by selecting a radio option and refreshing the entire screen, the live preview of the settings for the desired option will be displayed.
Is there a way to display the settings for a radio option as soon as it is selected so that I don’t have to refresh the full live preview screen every time I select a radio option?
The codes I put in the functions.php
file:
function customize_pro($wp_customize){
$wp_customize->add_section('Select_txt_radio', array(
'title' => esc_html__('Show or hide settings', 'section'),
'priority' => 5,
));
// Create a radio controller
$wp_customize->add_setting( 'setting_radio_main',
array(
'type' => 'theme_mod',
'transport' => 'refresh',
)
);
$wp_customize->add_control('setting_radio_main',
array(
'type' => 'radio',
'label' => esc_html__('Available options'),
'description' => esc_html__('By selecting each option, the corresponding settings will appear'),
'section' => 'Select_txt_radio',
'priority' => 1,
'choices' => array(
'ption1' => 'ption A',
'ption2' => 'ption B',
'ption3' => 'ption C',
),
)
);
// By selecting any option, the following terms apply
$select_control = get_theme_mod( 'setting_radio_main' ); // Radio configuration ID
if( $select_control === 'ption1' ) {
// Display the following settings if you select the first option
$wp_customize->add_setting( 'setting_radio_ption1',
array(
'type' => 'theme_mod',
'transport' => 'refresh',
)
);
$wp_customize->add_control( 'setting_radio_ption1',
array(
'section' => 'Select_txt_radio',
'type' => 'text',
'input_attrs' => array(
'placeholder' => __( 'ption1' ),
),
)
);
return;
} elseif( $select_control === 'ption2' ) {
// Display the following settings if you choose the second option
$wp_customize->add_setting( 'setting_radio_ption2',
array(
'type' => 'theme_mod',
'transport' => 'refresh',
)
);
$wp_customize->add_control( 'setting_radio_ption2',
array(
'section' => 'Select_txt_radio',
'type' => 'text',
'input_attrs' => array(
'placeholder' => __( 'ption2' ),
),
)
);
return;
} elseif( $select_control === 'ption3' ) {
// Display the following settings if you choose the third option
$wp_customize->add_setting( 'setting_radio_ption3',
array(
'type' => 'theme_mod',
'transport' => 'refresh',
)
);
$wp_customize->add_control( 'setting_radio_ption3',
array(
'section' => 'Select_txt_radio',
'type' => 'text',
'input_attrs' => array(
'placeholder' => __( 'ption3' ),
),
)
);
return;
} else{
// Display the following settings if no option is active
$wp_customize->add_setting( 'setting_radio_ptionNon',
array(
'type' => 'theme_mod',
'transport' => 'refresh',
)
);
$wp_customize->add_control( 'setting_radio_ptionNon',
array(
'section' => 'Select_txt_radio',
'type' => 'text',
'input_attrs' => array(
'placeholder' => __( 'default' ),
),
)
);
return;
}
}
add_action( 'customize_register', 'customize_pro' );
Edit:
I know that the page will be refreshed with the code 'transport' => 'refresh'
.
But when the page is refreshed, the settings of the desired option do not appear.
After selecting an option I have to click publish button in customization. and then refresh the live preview page through the full browser (refresh the page by updating its address); In this case, I can see the settings for that option.
I am looking for a solution so that when an option is selected, its settings will appear immediately and there is no need to click on the publish button and refresh the page through the browser.
The code I wrote has some bugs that I need help fixing and this code is just a rough idea.
2
Answers
I was able to solve my problem by searching the internet and using the following tutorial:
What’s new with the Customizer
Complete edited code:
Note: The code I wrote below is not exactly the code I wrote in the question above. This code has been edited, tested and works.
I hope this answer will be useful for friends who are looking for a solution to this problem.
You need to use the
active_callback
param when adding each of the conditional settings. Also, not sure ifption
is a misspelling ofoption
in your code example.