1

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
  }
  • In the active callback you can get the value of 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. – cybmeta May 15 '15 at 5:05
  • Actually, I am currently using script to show hide the field. Is it possible only using PHP? If it is possible then there would be no burden of loading extra script. – Nilambar Sharma May 15 '15 at 5:11
  • Yeah!!! You can do just with active_callback and WordPress will take care of the frontend part. See my answer. I've learned something new today. – cybmeta May 15 '15 at 6:05
2

You can get and check the value of your theme mod as you normally do anywhere.

This code is tested and working (the code inside cyb_customizer() is exactly the code you posted in the question, only added add_section part):

function flag_is_custom_excerpt_enabled(){
    $blueplanet_options = get_theme_mod( 'blueplanet_options');
    if( empty( $blueplanet_options['theme_enable_custom_excerpt'] ) ) {
        return false;
    }
    return true;
}

add_action( 'customize_register', 'cyb_customizer' );
function cyb_customizer( $wp_customize ) {


    $wp_customize->add_section(
        'admin_section',
        array(
            'title' => 'Admin section',
            'description' => 'Admin section',
            'priority' => 0,            
        )
    );

    $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',
          )
        );

    }
|improve this answer|||||
  • 1
    Its works like a charm. But, Not on live change. Is there any way to add dependency like active_callback onChange event though JS? – maheshwaghmare Nov 9 '16 at 10:57

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.