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?

  • 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 Jul 5 '17 at 15:01
5

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 )
        );
    },
) );
  • Perfect, thanks @Weston! You are the Customizer King ;) – Troy Templeman Jul 7 '17 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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