Customizer JS API

1) Maybe bind to the api.ready state which may fix having to call your section twice

(function($, api){
  api.bind( 'ready', function() {...

  }
})(jQuery);

I saw a note in trac that said “Note that the APIs for dynamically-added controls, and APIs for JS-templated custom Sections and Panels are not yet available as of WordPress 4.2. See #30741.” Reading that trac ends with “likely not for 4.5 right now” so your efforts may be futile =(

2) For reference, the wp_customize JS API can be found here. This link may be useful as well.

3) I don’t have enough rep for a third link but you might look at Kirki.org which is a helper framework for customizer fields. Kirki is pretty active on Github too.

4) On the PHP side, you can use the “active_callback” option on your field array to dynamically present fields.

$wp_customize->add_control( 'some_single_page_specific_option', array(
  'label'           => esc_html__( 'Single Page Option' ),
  'section'         => 'my_page_options',
  'active_callback' => 'if_is_singular',
));

function if_is_singular(){
  if( is_singular() ){
    return true;
  } else {
    return false;
  }
}

Good luck.

Leave a Comment