I have a piece of JS code that I use show/hide customizer control groups depending on a select item value.
The code was working fine until recent wp update (not sure which version first broke this).
Here is the code for enqueuing the js file.
add_action('customize_controls_print_scripts', 'ppl_customize_controls_scripts');
function ppl_customize_controls_scripts(){
wp_register_script( 'ppl-customize-control', get_template_directory_uri().'/js/customizer-control.js', array('jquery'), 1, true);
wp_enqueue_script( 'ppl-customize-control' );
}
The JS file
jQuery(document).ready(function($){
var skin_select = $('#customize-control-ppl-ninja-theme-options-skin').find('select');
//alert(skin_select.attr('class'));
var selected_val = skin_select.children('option:selected').val();
var custom_group = [
'#accordion-section-custom_header_styles',
'#accordion-section-custom_slider_styles',
'#accordion-section-custom_footer_styles',
'#accordion-section-custom_general_colors'
];
if(selected_val != 4){
hide_custom_controls(custom_group);
}
skin_select.change(function(){
var val = $(this).val();
if(val != 4){
hide_custom_controls(custom_group);
}else{
show_custom_controls(custom_group);
}
});
function hide_custom_controls(custom_group){
var selector = custom_group.join(',');
$(selector).hide();
}
function show_custom_controls(custom_group){
var selector = custom_group.join(',');
$(selector).show();
}
});
The problem is, JS loads and hides the customizer control section just fine with inline css display:none
. Then the cusomizer js script revises it and update the inline css to display:list-item
which makes them visible. So, I need to run the JS code later after the customizer loads fully.
I looked into customizer script in wp-includes directory but haven't find anything that I can use. Perhaps I missed something.
Further Information
The code that revising the css can be found in
/wp-admin/js/customize-controls.js
.The script is using the
ready
event to add class and revise visibility of panels, sections and controls. But I am unable to add listener to that event.The property activePanels, activeSections, activeControls are set from PHP from the file
/wp-admin/customize.php
I have found no way to overwrite that variable as you can see it is printed just before the body tag ends.
So, the possible solution is perhaps finding a way to hook into the ready
event.