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.

  • 1
    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 Oct 11 '19 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 Oct 11 '19 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 Oct 11 '19 at 16:21
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.

  • That's a good idea. Thankfully I didn't need to use it, but I'll keep it in mind for the future. – Jeremiah Wolfe Oct 11 '19 at 16:23
  • very handy to know this one, for example if you are in the unfortunate place of having to apply a fix a 3rd party script, you can more easily point to a different source URL with the fix and have it working (until the developer hopefully incorporates the fix you reported in the next version.right? lol) – majick Oct 11 '19 at 16:34
0

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 );

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.