Dequeue Woocommerce Scripts on Unnecessary Pages

An even more thorough version of this method can be found here.

Woocommerce is great!

For the past couple of years I’ve been using it for nearly every e-commerce project I’ve worked on, including heavy hitter Rotating Mass Media‘s store (the parent company of famously popular Dirt Rag Magazine and sister Bicycle Times Magazine).

But, as much as I like it, I’m not sure why they feel the need to load their scripts on every page of a WordPress website. What do I mean by that?

Well, Woocommerce loads about six scripts which handle various functionality (maybe more, maybe less depending on your version and the settings you’re using). By “scripts”, I mean Javascripts. However, many pages on a WordPress site will have nothing to do with Woocommerce. Perhaps they do this because you might have some widgets installed on sidebars which continue to appear on non-Woo-related posts and pages. If you don’t, though, you need not include all of these scripts on your site. They’ll slow your site down and possibly result in decreased search engine rankings. They’ll definitely get you a lower score on PageSpeed.

Fortunately, we can remove those scripts from unnecessary pages. Add the following to your theme’s functions.php file:

// remove scripts on unnecessary pages
function ply_de_script() {
if ( function_exists( 'is_woocommerce' ) && !is_woocommerce() && !is_cart() && !is_checkout() && !is_account_page() ) {
add_action( 'wp_print_scripts', 'ply_de_script', 100 );

Note that the is_woocommerce() function won’t cover your Cart or Checkout pages, as those are just normal Pages and not Woo-specific…so, you’d need to determine those Page IDs and bonus points to whoever can modify the code above and post it in the comments.

Thanks to commenter Ruben Bristian for uncovering some additional Woocommerce conditional tags that make this a quick and easy copy/paste into your functions.php, no need to worry about sorting things out for Cart and Checkout page IDs.

Up Next: Reflections on Pagespeed Insights: Minify HTML?