Skip to content

Instantly share code, notes, and snippets.

@wpgaurav
Created September 21, 2022 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpgaurav/fac07c3d03b4733f853b139baf2741b5 to your computer and use it in GitHub Desktop.
Save wpgaurav/fac07c3d03b4733f853b139baf2741b5 to your computer and use it in GitHub Desktop.
Load WooCommerce Scripts only When Needed
<?php add_action('wp_enqueue_scripts', 'myown_remove_scripts');
function myown_remove_scripts(){
if ( function_exists( 'is_woocommerce' ) ) {
//dequeue scripts and styles from not WC pages
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
wp_dequeue_style( 'woocommerce-layout');
wp_dequeue_style( 'woocommerce-smallscreen');
wp_dequeue_style( 'woocommerce-general');
wp_dequeue_script( 'wc_price_slider' );
wp_dequeue_script( 'wc-single-product' );
wp_dequeue_script( 'wc-add-to-cart' );
wp_dequeue_script( 'wc-cart-fragments' );
wp_dequeue_script( 'wc-checkout' );
wp_dequeue_script( 'wc-add-to-cart-variation' );
wp_dequeue_script( 'wc-single-product' );
wp_dequeue_script( 'wc-cart' );
wp_dequeue_script( 'wc-chosen' );
wp_dequeue_script( 'woocommerce' );
}
}
}
?>
@wpgaurav
Copy link
Author

WooCommerce is useful, but a very heavy plugin. It enqueues its files on every page of your site, whether needed or not. By simply copying & pasting the code into your theme‘s functions.php file, you can make sure that WooCommerce scripts load only on WooCommerce pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment