Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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