EDIT - SOLUTION GIVEN IN COMMENTS
The problem was that I was initially hooking in at wp_enqueue_scripts so I wanted a high priority to make sure my dequeue was happening after the plugin's enqueue. When I switched to using wp_print_scripts (or more accuratly wp_print_footer_scripts) I should have switched to a low priority, as Jacob Peattie pointed out. That was all I needed to fix this. -JW
I'm trying to dequeue a script which is added by a plugin. Here is the code from the plugin...
class WC_Social_Login_Frontend {
...
public function load_styles_scripts() {
...
wp_enqueue_script( 'wc-social-login-frontend', wc_social_login()->get_plugin_url() . '/assets/js/frontend/wc-social-login.min.js', $script_deps, WC_Social_Login::VERSION, $load_in_footer );
}
}
Nothing I have tried, thusfar, has stopped this script from loading. Here is my code which is inside of a mu-plugin I used instead of functions.php. I've tried hooking in at various places, none have worked.
function jw_dequeue_social_login(){
wp_dequeue_script('wc-social-login-frontend');
wp_deregister_script('wc-social-login-frontend');
}
add_action( 'wp_print_footer_scripts', 'jw_dequeue_social_login', PHP_INT_MAX );
add_action( 'wp_print_scripts', 'jw_dequeue_social_login', PHP_INT_MAX );
add_action( 'wp_enqueue_scripts', 'jw_dequeue_social_login', PHP_INT_MAX );
add_action( 'wp_footer', 'jw_dequeue_social_login', PHP_INT_MAX );
Hopefully I'm an idiot and have missed something obvious. Your help is greatly appreciated.
EDIT - SOLUTION GIVEN IN COMMENTS
The problem was that I was initially hooking in at wp_enqueue_scripts so I wanted a high priority to make sure my dequeue was happening after the plugin's enqueue. When I switched to using wp_print_scripts (or more accuratly wp_print_footer_scripts) I should have switched to a low priority, as Jacob Peattie pointed out. That was all I needed to fix this. -JW
I'm trying to dequeue a script which is added by a plugin. Here is the code from the plugin...
class WC_Social_Login_Frontend {
...
public function load_styles_scripts() {
...
wp_enqueue_script( 'wc-social-login-frontend', wc_social_login()->get_plugin_url() . '/assets/js/frontend/wc-social-login.min.js', $script_deps, WC_Social_Login::VERSION, $load_in_footer );
}
}
Nothing I have tried, thusfar, has stopped this script from loading. Here is my code which is inside of a mu-plugin I used instead of functions.php. I've tried hooking in at various places, none have worked.
function jw_dequeue_social_login(){
wp_dequeue_script('wc-social-login-frontend');
wp_deregister_script('wc-social-login-frontend');
}
add_action( 'wp_print_footer_scripts', 'jw_dequeue_social_login', PHP_INT_MAX );
add_action( 'wp_print_scripts', 'jw_dequeue_social_login', PHP_INT_MAX );
add_action( 'wp_enqueue_scripts', 'jw_dequeue_social_login', PHP_INT_MAX );
add_action( 'wp_footer', 'jw_dequeue_social_login', PHP_INT_MAX );
Hopefully I'm an idiot and have missed something obvious. Your help is greatly appreciated.
Share Improve this question edited Oct 11, 2019 at 16:28 Jeremiah Wolfe asked Oct 11, 2019 at 8:33 Jeremiah WolfeJeremiah Wolfe 514 bronze badges 3 |2 Answers
Reset to default 3Thanks to Jacob Peattie for answering this in the comments!
The problem was using a high priority with wp_print_footer_scripts when I should have been using a low priority.
function jw_dequeue_social_login(){
wp_dequeue_script('wc-social-login-frontend');
wp_deregister_script('wc-social-login-frontend');
}
add_action( 'wp_print_footer_scripts', 'jw_dequeue_social_login', 0 );
Another option is to use the script_loader_tag
filter to catch and remove the actual HTML tag before it is output, eg.:
add_filter( 'script_loader_tag', 'remove_social_login_frontend', 10, 3 );
function remove_social_login_frontend( $tag, $handle, $src ) {
if ( 'wc-social-login-frontend' === $handle ) {$tag = '';}
return $tag;
}
Note you could also do this via a string match on the URL via the $src
variable.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745089181a4610585.html
load_styles_scripts
is hooked in the plugin? Somewhere should be something likeadd_action( '???', array( $this, 'load_styles_scripts' ) );
, but$this
might be something else, or missing. The important thing is finding theadd_action()
that runsload_styles_scripts
. – Jacob Peattie Commented Oct 11, 2019 at 8:47PHP_INT_MAX
will be too late forwp_print_footer_scripts
,wp_print_scripts
andwp_footer
hooks. If the original function is run before those hooks, then the script will likely already be printed by the time your function for de-queueing them runs. Try changing those to0
. – Jacob Peattie Commented Oct 11, 2019 at 8:49