最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Unable to dequeue a plugin script. I think I've tried everything

programmeradmin0浏览0评论

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 Can you find where load_styles_scripts is hooked in the plugin? Somewhere should be something like add_action( '???', array( $this, 'load_styles_scripts' ) );, but $this might be something else, or missing. The important thing is finding the add_action() that runs load_styles_scripts. – Jacob Peattie Commented Oct 11, 2019 at 8:47
  • Also, PHP_INT_MAX will be too late for wp_print_footer_scripts, wp_print_scripts and wp_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 to 0. – Jacob Peattie Commented Oct 11, 2019 at 8:49
  • Thanks @JacobPeattie ! That was the problem and now I feel like an idiot. But I'd rather feel like an idiot and have the problem solved...so thanks. I was initially hooking in at wp_enqueue_scripts so I kept pushing the priority higher and higher then when I switched to using wp_print_scripts I still had 'must use high priority' in my brain. Changing it to 0 solved the problem. – Jeremiah Wolfe Commented Oct 11, 2019 at 16:21
Add a comment  | 

2 Answers 2

Reset to default 3

Thanks 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.

发布评论

评论列表(0)

  1. 暂无评论