Dequeue Scripts in WordPress Not Working – Fixed

Dequeue Scripts in WordPress Not Working?

Dequeuing and Deregistering script is a valid way to speed up your site.  Without a speedy site, you will get all sorts of red & yellow flags from Google Ads and Google Search Console.

Dequeue will rarely work 100% on the first try.  It’s not you, it’s just convoluted.  The hints from Google’s PageSpeed Insights often leads you very little gains.  I’m after the real stuff here; a 30-40% BOOST in the score, which I’ve gotten on multiple WordPress-WooCommerce sites now.

 

#1 Caching Services maybe blocking the Dequeue Process for CSS / JS

In my case, I disabled CSS minification offered default from Kinsta.  I’ve never found CSS minification to make a big 5% difference.  Wasn’t worth it.  Your mileage may vary.

 

#2 WP_head holds a shoreline for some plugins to invade with their scripts.

You NEED Query Monitor to really get the real name (“handle”) of the script you need to block.  Google will not help you here.  In fact, most of Google’s own scripts/plugins will be the first you should let go of.  Devin Price wrote the initial code that didn’t work for me, so I deep dove to figure things out.  AutoOptimize only improved things by 3% on mobile so I decided it wasn’t worth a plugin for.

Before doing anything, please do BACKUP your things.

I’m going to explain this better one day, but for now,

I’m just showing you how to NUKE OUT THESE JUNK SCRIPTS FROM ORBIT

 

Code below is ugly and I’ll update it at my leisure.  Pretty sure my !is_page logic is wrong for Products pages too.  TBD.

/* Remove scripts that have been enqueued by other plugins. PETER 2023 */
function prefix_remove_scripts() {
    if ( !is_page(‘product’) or !is_page(‘checkout’) or !is_page(‘my-account’) or !is_page(‘cart’) or !is_page(‘product-category’) or !is_page(‘wp-admin’) or !is_page(‘secure-login’) ) {
        //DISABLE UNNECESSARY SCRIPTS EVERYWHERE EXCEPT WOOCOMMERCE PAGES
        //BROAD AREA DISABLE:
        wp_dequeue_script( ‘woocommerce’ );
        wp_dequeue_script( ‘wc-add-to-cart’ );
        wp_dequeue_script( ‘wc-cart-fragments’ );
        wp_dequeue_script( ‘storefront-header-cart’ );
        wp_dequeue_script( ‘woo-tracks’ );
        wp_deregister_script (‘woo-tracks’);
        wp_dequeue_script( ‘pipdig-fitvids’ );
        wp_deregister_script (‘pipdig-fitvids’);
        wp_dequeue_script( ‘photoswipe’ );
        wp_deregister_script (‘photoswipe’);
        wp_dequeue_script( ‘photoswipe-ui-default’ );
        wp_deregister_script (‘photoswipe-ui-default’);
      if ( is_front_page() or is_page(‘linktree’) or is_page(‘links’) or is_page(‘me’) or is_page(‘youtube-links’) )
      {
        //only disable if, YES ITS ON HOME PAGE OR LINKS STYLE PAGES
        //disabling sharing and fluffy stuff
        wp_dequeue_script( ‘heateor_sss_sharing_js’ );
        wp_deregister_script (‘heateor_sss_sharing_js’);
        wp_dequeue_script( ‘algolia-autocomplete’ );
        wp_deregister_script (‘algolia-autocomplete’);
        wp_dequeue_script( ‘wp-polls’ );
        wp_deregister_script (‘wp-polls’);
        //GOOGLE SUBSCRIBE BUTTON at end of every POST
        wp_dequeue_script( ‘google’ );
        wp_deregister_script (‘google’);
      } //if
    } //if
}
add_action( ‘wp_print_scripts’, ‘prefix_remove_scripts’, 100 );
add_action( ‘wp_head’, ‘prefix_remove_scripts’, 100 );
/**
 * Remove styles that have been enqueued by other plugins. PETER 2023
 */
function prefix_remove_styles() {
    if ( is_front_page() or !is_page(‘product’) or !is_page(‘checkout’) or !is_page(‘my-account’) or !is_page(‘cart’) or !is_page(‘product-category’) or !is_page(‘wp-admin’) or !is_page(‘secure-login’)) {
        wp_dequeue_style( ‘wp-block-library’ );
        wp_dequeue_style( ‘wc-block-vendors-style’ );
        wp_dequeue_style( ‘wc-block-style’ );
        wp_dequeue_style( ‘woocommerce-inline’ );
        //DISABLE EVERYWHERE EXCEPT WOOCOMMERCE PAGES PART 1
        wp_dequeue_style( ‘wc-blocks-style-mini-cart’ );
        wp_deregister_style(‘wc-blocks-style-mini-cart’);
        wp_dequeue_style( ‘wc-blocks-style-cart’ );
        wp_deregister_style(‘wc-blocks-style-cart’);
        wp_dequeue_style( ‘wc-blocks-style-attribute-filter’ );
        wp_deregister_style(‘wc-blocks-style-attribute-filter’);
        wp_dequeue_style( ‘wc-blocks-style-price-filter’ );
        wp_deregister_style(‘wc-blocks-style-price-filter’);
        wp_dequeue_style( ‘wc-blocks-style-price-filter’ );
        wp_deregister_style(‘wc-blocks-style-checkout’);
        wp_dequeue_style( ‘wc-blocks-style-checkout’ );
        wp_deregister_style(‘wc-blocks-style-mini-cart-contents’);
        wp_dequeue_style( ‘wc-blocks-style-mini-cart-contents’);
        wp_deregister_style(‘wc-blocks-style-stock-filter’);
        wp_dequeue_style( ‘wc-blocks-style-stock-filter’);
        wp_deregister_style(‘wc-blocks-style-product-details’);
        wp_dequeue_style( ‘wc-blocks-style-product-details’);
        wp_deregister_style(‘wc-blocks-style-reviews-by-product’);
        wp_dequeue_style( ‘wc-blocks-style-reviews-by-product’);
        wp_deregister_style(‘wc-blocks-style-reviews-by-category’);
        wp_dequeue_style( ‘wc-blocks-style-reviews-by-category’);
        wp_deregister_style(‘wc-blocks-style-product-summary’);
        wp_dequeue_style( ‘wc-blocks-style-product-summary’);
        wp_deregister_style(‘wc-blocks-style-product-sku’);
        wp_dequeue_style( ‘wc-blocks-style-product-sku’);
        wp_dequeue_style( ‘wc-blocks-style-product-query’ );
        wp_dequeue_style( ‘wc-blocks-style-product-image-gallery’ );
wp_dequeue_style( ‘wc-blocks-style-product-results-count’ );
wp_dequeue_style( ‘wc-blocks-style-product-image’ );
wp_dequeue_style( ‘wc-blocks-style-product-categories’ );
wp_dequeue_style( ‘wc-blocks-style-product-button’ );
wp_dequeue_style( ‘wc-blocks-style-product-add-to-cart’ );
wp_dequeue_style( ‘wc-blocks-style-product-reviews’ );
wp_dequeue_style( ‘wc-blocks-style-product-results-count’ );
wp_dequeue_style( ‘wc-blocks-style-product-query’ );
wp_dequeue_style( ‘wc-blocks-style-product-image-gallery’ );
wp_dequeue_style( ‘wc-blocks-style-product-image’ );
wp_dequeue_style( ‘wc-blocks-style-product-categories’ );
wp_dequeue_style( ‘wc-blocks-style-product-button’ );
wp_dequeue_style( ‘wc-blocks-style-featured-product’ );
wp_dequeue_style( ‘wc-blocks-style-featured-category’ );
wp_dequeue_style( ‘wc-blocks-style-customer-account’ );
wp_dequeue_style( ‘wc-blocks-style-catalog-sorting’ );
wp_dequeue_style( ‘wc-blocks-style-breadcrumbs’ );
wp_dequeue_style( ‘wc-blocks-style-attribute-filter’ );
wp_dequeue_style( ‘wc-blocks-style-all-reviews’ );
wp_dequeue_style( ‘wc-blocks-style-all-products’ );
wp_dequeue_style( ‘wc-blocks-packages-style’ );
wp_dequeue_style( ‘wc-blocks-style-add-to-cart-form’ );
wp_dequeue_style( ‘wc-blocks-style-active-filters’ );
wp_dequeue_style( ‘wc-blocks-style’ );
wp_dequeue_style( ‘wc-blocks-style-product-sale-badge’ );
wp_dequeue_style( ‘wc-blocks-style-rating-filter’ );
wp_deregister_style( ‘wc-blocks-style-customer-account’ );
wp_deregister_style( ‘wc-blocks-style-product-query’ );
wp_deregister_style( ‘wc-blocks-style-product-image-gallery’ );
wp_deregister_style( ‘wc-blocks-style-product-results-count’ );
wp_deregister_style( ‘wc-blocks-style-product-image’ );
wp_deregister_style( ‘wc-blocks-style-product-categories’ );
wp_deregister_style( ‘wc-blocks-style-product-button’ );
wp_deregister_style( ‘wc-blocks-style-product-add-to-cart’ );
wp_deregister_style( ‘wc-blocks-style-product-reviews’ );
wp_deregister_style( ‘wc-blocks-style-product-results-count’ );
wp_deregister_style( ‘wc-blocks-style-product-query’ );
wp_deregister_style( ‘wc-blocks-style-product-image-gallery’ );
wp_deregister_style( ‘wc-blocks-style-product-image’ );
wp_deregister_style( ‘wc-blocks-style-product-categories’ );
wp_deregister_style( ‘wc-blocks-style-product-button’ );
wp_deregister_style( ‘wc-blocks-style-featured-product’ );
wp_deregister_style( ‘wc-blocks-style-featured-category’ );
wp_deregister_style( ‘wc-blocks-style-customer-account’ );
wp_deregister_style( ‘wc-blocks-style-catalog-sorting’ );
wp_deregister_style( ‘wc-blocks-style-breadcrumbs’ );
wp_deregister_style( ‘wc-blocks-style-attribute-filter’ );
wp_deregister_style( ‘wc-blocks-style-all-reviews’ );
wp_deregister_style( ‘wc-blocks-style-all-products’ );
wp_deregister_style( ‘wc-blocks-packages-style’ );
wp_deregister_style( ‘wc-blocks-style-add-to-cart-form’ );
wp_deregister_style( ‘wc-blocks-style-active-filters’ );
wp_deregister_style( ‘wc-blocks-style’ );
wp_deregister_style( ‘wc-blocks-style-product-sale-badge’ );
wp_deregister_style( ‘wc-blocks-style-rating-filter’ );
      //DISABLE OTHER STUFF TOO
//wp_dequeue_style( ‘cr-badges-css’ );
wp_dequeue_style( ‘wc-blocks-style-cart’ );
wp_dequeue_style( ‘ivole-frontend-css’ );
wp_deregister_style( ‘wc-blocks-style-cart’ );
wp_deregister_style( ‘ivole-frontend-css’ );
wp_dequeue_style( ‘algolia-autocomplete’ );
wp_deregister_style( ‘algolia-autocomplete’ );
wp_dequeue_style( ‘photoswipe’ );
wp_deregister_style( ‘photoswipe’ );
wp_dequeue_style( ‘photoswipe-default-skin’ );
wp_deregister_style( ‘photoswipe-default-skin’ );
wp_dequeue_style( ‘wc-all-blocks-style’ );
wp_deregister_style( ‘wc-all-blocks-style’ );
wp_dequeue_style( ‘wc-blocks-vendors-style’ );
wp_deregister_style( ‘wc-blocks-vendors-style’ );
      //DISABLE JUST ON LINKTREE BUT KEEP FOR OTHERS
      if ( is_page(‘linktree’) or is_page(‘links’) or is_page(‘me’) or is_page(‘about’) )
      {
        //this part maybe for mobile menu, can’t delete
        //wp_dequeue_style( ‘pipdig-style’ );
        // wp_deregister_style( ‘pipdig-style’ );
         wp_dequeue_style( ‘p3-core-responsive’ );
         wp_deregister_style( ‘p3-core-responsive’ );
         wp_dequeue_style( ‘heateor_sss_frontend_css’ );
         wp_deregister_style( ‘heateor_sss_frontend_css’ );
        }
    }
}
add_action( ‘wp_enqueue_scripts’, ‘prefix_remove_styles’, 100 );
add_action( ‘wp_head’, ‘prefix_remove_styles’, 100 );
/* PP stuff ends ^^ */

Dequeueing Scripts in WordPress Can Break You Whole Site

This is an obvious disclaimer and only experienced developers should be searching up this blog post anyway.

GOOD LUCK!

 

Dequeueing Scripts and Styles Result for this Website

This score doesn’t matter much does it.  Try to put Google PageSpeed Insights score on Apple.com for a chuckle.

Nevertheless, this is something we can control and improve.

Please note a Google PageSpeed Insights will vary from time to time, but I’m happy with it.  I can FEEL the difference and that also matters to me.

wordpress pagespeed insights optimization

Peter Peng

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart