2

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
  • Can you, for this example, not combine the two callback functions into one? Your two callback functions seem simple enough. I dont know if you can add multiple active_callback, adding them like you did however makes little sense. Also if you define multiple callbacks, how should the system know which one to use and when? So it has to call every defined callback first to see if the conditional match.
    – LWS-Mo
    Commented Jul 5, 2017 at 15:01

1 Answer 1

6

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 )
        );
    },
) );
1
  • Perfect, thanks @Weston! You are the Customizer King ;) Commented Jul 7, 2017 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.