1
add_action( 'after_setup_theme', 'wdwt_customizer_register' );
function wdwt_customizer_register(){
  add_action( 'customize_register', 'wdwt_customizer_add_panels' );
}
function wdwt_customizer_add_panels($wp_customize ){
  /*this function is called twice*/
}

The same is also true for twentyfifteen theme. twentyfifteen_customize_register function hooked on the same tag, is called twice.

It seems that the action 'customize_register' is being done twice, once in customizer admin, and once in preview part. WordPress version is 4.3.

Is this bug or a feature ?

| improve this question | | | | |
  • This wdwt_customizer_register() function is not part of the WordPress core, so I assume it ships with your theme. – birgire Sep 3 '15 at 8:04
  • Yes, this is theme code. But why customize_register action hook is called twice? All this code is just included in theme and is loaded unconditionally. – progmastery Sep 3 '15 at 9:51
1

I think the customizer creates an xhr request for to load the site preview( see devtools under the network > xhr tab ). In my case I want to check if a certain variable isset via the $_GET['foo'] variable. My function must be called once and not twice otherwise it will override my previous state.

My solution is to check if the $_POST['wp_customize'] it's empty or not. If empty that means that my function doesn't get called twice by preview request.

I use the following hook/action customize_loaded_components

Example:

function wpdocs_remove_widgets_panel( $components ) {
    if( empty( $_POST['wp_customize'] ) ) {
       // Do stuff here that will not be loaded twice by the previewer
    }
    return $components;
}

add_filter( 'customize_loaded_components', 'wpdocs_remove_widgets_panel' );
| improve this answer | | | | |

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.