Remove WooCommerce Scripts Outside of WooCommerce


About The Author
Avatar Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







Remove WooCommerce Scripts Outside of WooCommerce

For people that browse WP Cover frequently, you’ll notice a lot of articles end with “without a plugin”.  This is because my theory as a developer is to avoid unnecessary plugins at all costs.  I’m not saying I don’t use plugins, I definitely do and there’s 2-3 that I pretty much use on every WordPress website I develop, they are:

  • Advanced Custom Fields
  • Gravity Forms
  • Yoast SEO

 

Outside of that, I will use plugins when and where I feel the plugin was well written or if time and/or budget prevents the use of something custom.  A big issue with plugins the young WordPress users tend to ignore is how many assets each add to the site.  I inherited a while back where the owner was complaining about site load times.  I took my first look at the site and they had over 55 plugins installed, are you fu**ing kidding?!  Needless to say, I told the owner that if he wanted me to work on the site, it would have to be re-developed from the ground up, you just can’t fix 55 plugins.  However, one way you could go about reducing the assets loaded for plugins like WooCommerce is to dequeue specific plugin scripts or stylesheets and only load them when actually needed.  Below is an example of how you would dequeue WooCommerce assets if the page is a woocommerce page, the cart, or the checkout page:

 

function load_woo_scripts_styles(){
	
if( function_exists( 'is_woocommerce' ) ){
    // Only load styles/scripts on Woocommerce pages   
	if(! is_woocommerce() && ! is_cart() && ! is_checkout() ) { 		
		
		// Dequeue scripts.
		wp_dequeue_script('woocommerce'); 
		wp_dequeue_script('wc-add-to-cart'); 
		wp_dequeue_script('wc-cart-fragments');
		
		// Dequeue styles.	
		wp_dequeue_style('woocommerce-general'); 
		wp_dequeue_style('woocommerce-layout'); 
		wp_dequeue_style('woocommerce-smallscreen'); 
			
		}
	}	
}

add_action( 'wp_enqueue_scripts', 'load_woo_scripts_styles');

 

As you can see, we’re first checking to see if WooCommerce is activated with the function_exists ‘if’ statement. Next, we’re checking to see if we’re on a WooCommerce page where the assets need to be loaded. If we’re not, then we’re dequeuing the WooCommerce scripts as they are not necessary.

Overall, less assets mean faster load time and hopefully better SEO and user experience. Enjoy!


Share Your Thoughts

Leave a Reply

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


Related Stuff You Might Like







20% Off WP Engine

WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!