0

I have a javascript which reloads the page when content change is detected. This is the behaviour I want on the main site. But when I use the Wordpress Customizer I don't want this behaviour as it makes it very hard to work and Customize the page (the page tries to reload).

I tried deregistering the script and create a copy of the script without the reload function. This is how I did it:

add_action( 'wp_enqueue_scripts', 'customize_enqueue_appc_js' );
function customize_enqueue_appc_js() {
   if ( is_customize_preview() ) {
        wp_deregister_script( 'app-js' );
        wp_enqueue_script( 'appc-js', get_template_directory_uri() . '/javascripts/appc.js', array('jquery'), false, true );
   }
}

My idea here is to detect if you are on customizer with if ( is_customize_preview() ) { and if true, deregister the javascript called app-js, and enqueue the script appc-js which hasn't got the reload function.

But this doesn't seem to work. Is it because the Customizer views the page without modifications? Are you able to do something like this? Modifying the page functions when viewed in Customizer?

Is there any other approach to this? Like doing this in jQuery?

  • 1
    What you've done should work. How is the original script enqueued? Make sure your hook that deregisters the script runs after it's been registered by running it at a later priority. – Jacob Peattie Nov 14 '18 at 9:55
  • The priority might be the problem! I just changed it to 999 and it seems to work. Will be doing some testing! Thank you @JacobPeattie – joq3 Nov 14 '18 at 9:58
0

add_action have 4 parameters, the third one being priority ($priority). Usually plugins and child themes load their scripts after a parent theme to ensure that it doesn't get overrun later on. So it is wise and good practice to run your action dead last. To to this, you will need a very low priority, ie very high number.

add_action( 'wp_enqueue_scripts', 'jquery_loadup', 999 );

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.