admin管理员组文章数量:1130349
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 );
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 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 Commented Nov 14, 2018 at 9:55
-
The priority might be the problem! I just changed it to
999and it seems to work. Will be doing some testing! Thank you @JacobPeattie – joq3 Commented Nov 14, 2018 at 9:58
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 );
本文标签: phpRemove specific javascript when viewing page in Customizer
版权声明:本文标题:php - Remove specific javascript when viewing page in Customizer? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749182210a2328799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


999and it seems to work. Will be doing some testing! Thank you @JacobPeattie – joq3 Commented Nov 14, 2018 at 9:58