0

I have a conflict between two plugins that both load the same script I'm trying to resolve. I'm attempting to do so by checking if both have been loaded, and then if that's true, deregistering one of them. However, one of the scripts is always loading after the last script in wp_enqueue_scripts, no matter how high I set the priority value.

// account for conflicts between GiveWP Stripe AND WooCommerce Stripe script loading
function squarecandy_stripe_resolve() {
    if ( wp_script_is('stripe') && wp_script_is('give-stripe-js') ) :
        // if both scripts were loaded, remove one of them
        wp_dequeue_script( 'give-stripe-js' );
    else :
        // this is just to test what's going wrong...
        print 'stripe queued: ' . var_export(wp_script_is('stripe','to_do'),true);
        print 'give-stripe-js queued: ' . var_export(wp_script_is('give-stripe-js'),true);
    endif;
}
add_action( 'wp_enqueue_scripts', 'squarecandy_stripe_resolve', 999999999 );

The script give-stripe-js is being properly identified as loaded and appears above the just-troubleshooting line in the HTML head. But the WooCommerce Stripe script loads one line after my code and doesn't register as enqueued in my script.

Digging into the WooCommerce source I found:

class WC_Stripe_Payment_Request {
    public function __construct() {
        add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
        // ...etc

What's going on here? I can't find anything in the documentation about add_action with an array passed within a class... maybe that has something to do with it? How can I do a check to see if both of these scripts have loaded after all plugins have had a chance to enqueue but before anything gets printed to the <head>?

  • the array is used to specify a class method - in this case it is the 'scripts' method of the object $this. you can try using the 'to_do' list option (the second parameter of wp_script_is). – inarilo Oct 5 '17 at 6:07
  • print var_export(wp_script_is('stripe','to_do'),true); This is still returning "false" in my function... – squarecandy Oct 5 '17 at 18:39
  • Also testing with ` 'enqueued', 'registered', 'queue', 'to_do', and 'done' ` in the second parameter of wp_script_is. – squarecandy Oct 5 '17 at 18:55
0

wp_deregister_script only removes the registration associated with the handle, and it will not remove the item from the queue itself. To remove it from the queue you should call wp_dequeue_script

  • Hi Mark - thanks, I think you are correct that wp_dequeue_script is what I should be using... However, that line of code is not even firing yet. What I'm really trying to figure out is why add_action( 'wp_enqueue_scripts') with the priority set to 999999999 in my theme is still loading before one of the plugin's scripts, which are also added with add_action( 'wp_enqueue_scripts') – squarecandy Oct 5 '17 at 18:50
  • See updates in the question. The current if: statement does not pass, and the lines in the else: section are outputting: stripe queued: false give-stripe-js queued: true – squarecandy Oct 5 '17 at 18:52
  • then it is enqueued later than you think, not with the handle you think, or not via that action at all. you will need to debug the woocomerce code to see when the enque being done, at least in relation to your code. In general since scripts are usually output at the footer there is no rush in dequing them on the enqueue action and it can be done later )early on wp_footer for example`) if the enqueue proves to happen later. – Mark Kaplun Oct 6 '17 at 2:11

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.