WordPress Customizer: How can you have multiple active callbacks on one control?

I have the following two Customizer controls that displays a checkbox for showing/hiding content. Each checkbox is followed by an active_callback that shows/hides the corresponding controls when the checkbox is checked/unchecked.

// Display About Block Button
$wp_customize->add_setting( 'display_about_block_button', array(
    'default'           => true
) );
$wp_customize->add_control( 'display_about_block_button', array(
    'label'             => __( 'Display About Button', 'my_theme_name' ),
    'type'              => 'checkbox',
    'section'           => 'about',
    'active_callback'   => 'display_about_block_callback'
) );
function display_about_block_button_callback( $control ) {
    if ( $control->manager->get_setting( 'display_about_block_button' )->value() == true ) {
        return true;
    } else {
        return false;
    }
}

// Display About Block
$wp_customize->add_setting( 'display_about_block', array(
    'default'           => true
) );
$wp_customize->add_control( 'display_about_block', array(
    'label'             => __( 'Display About Block', 'my_theme_name' ),
    'type'              => 'checkbox',
    'section'           => 'about'
) );
function display_about_block_callback( $control ) {
    if ( $control->manager->get_setting( 'display_about_block' )->value() == true ) {
        return true;
    } else {
        return false;
    }
}

They both work alone except the problem is the About Block Button below is inside the About Block. I want to be able to hide the About Block Button alone if the About Block is displayed but also be able to hide the About Block Button if the About Block is hidden altogether (without having to check both boxes). Therefore, it needs two active_callback‘s like this:

// About Block Button Text
$wp_customize->add_setting( 'about_block_button_text', array(
    'default'           => __( 'Read More', 'my_theme_name' )
) );    
$wp_customize->add_control( 'about_block_button_text', array(
    'label'             => __( 'About Button Text', 'my_theme_name' ),
    'type'              => 'text',
    'section'           => 'about',
    'active_callback'   => 'display_about_block_button_callback',
    'active_callback'   => 'display_about_block_callback'
) );

Unfortunately, it doesn’t accept both active_callback‘s. How can I have two (or possibly more) active callbacks on one control?

1 Answer
1

You can create a third active callback function that just references the two existing ones, for example via an anonymous function (PHP≥5.3):

// About Block Button Text
$wp_customize->add_setting( 'about_block_button_text', array(
    'default'           => __( 'Read More', 'my_theme_name' )
) );    
$wp_customize->add_control( 'about_block_button_text', array(
    'label'             => __( 'About Button Text', 'my_theme_name' ),
    'type'              => 'text',
    'section'           => 'about',
    'active_callback'   => function( $control ) {
        return ( 
            display_about_block_button_callback( $control )
            &&
            display_about_block_callback( $control )
        );
    },
) );

Leave a Comment