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?
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?
Share Improve this question asked Nov 14, 2018 at 9:53 joq3joq3 3813 silver badges21 bronze badges 2 |1 Answer
Reset to default 0add_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 );
999
and it seems to work. Will be doing some testing! Thank you @JacobPeattie – joq3 Commented Nov 14, 2018 at 9:58