I am trying to have dependent field in Customizer. One field is checkbox Enable Custom Excerpt Length
. Another is text field Custom Excerpt Length
. I want to implement contextual field using active_callback
. I am following this article. https://make.wordpress.org/core/2014/07/08/customizer-improvements-in-4-0/
I am having problem in checking control value in callback function.
$wp_customize->add_setting( 'blueplanet_options[theme_enable_custom_excerpt]',
array(
'default' => false,
'capability' => 'edit_theme_options',
)
);
$wp_customize->add_control(
'theme_enable_custom_excerpt',
array(
'label' => 'Enable Custom Excerpt Length',
'section' => 'admin_section',
'settings' => 'blueplanet_options[theme_enable_custom_excerpt]',
'type' => 'checkbox',
)
);
$wp_customize->add_setting( 'blueplanet_options[theme_custom_excerpt_length]',
array(
'default' => 20,
'capability' => 'edit_theme_options',
)
);
$wp_customize->add_control(
'theme_custom_excerpt_length',
array(
'label' => 'Custom Excerpt Length',
'section' => 'admin_section',
'settings' => 'blueplanet_options[theme_custom_excerpt_length]',
'type' => 'text',
'active_callback' => 'flag_is_custom_excerpt_enabled',
)
);
// Callback function
function flag_is_custom_excerpt_enabled(){
// how to check if `theme_enable_custom_excerpt` is enabled or disabled
}
theme_enable_custom_excerpt
as you normally do anywhere. The problem is that the active callback is in PHP, so you need to save and (not sure) reload the page to see the effect. I think you can keep the active callback function to render or not the excerpt length field on page load; then use CSS and JavaScript to show/hide the excerpt lenght field based on user interaction with the checkbox.active_callback
and WordPress will take care of the frontend part. See my answer. I've learned something new today.