Skip to content

Instantly share code, notes, and snippets.

@bhwebworks
Forked from jan-koch/remove-woo-scripts.php
Created April 11, 2020 21:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bhwebworks/2c9dc35623606b85a6b6605dbca6cc81 to your computer and use it in GitHub Desktop.
Remove WooCommerce related resources except on WooCommerce-based pages (products, cart, checkout, etc). Use on a testing environment before pasting this into your live website!
/**
* This code snippet removes JavaScript and CSS files loaded from WooCommerce if they are not necessary.
*
* Please test this on a staging copy of your website before putting this into the functions.php of your live website.
*/
add_action( 'wp_enqueue_scripts', 'my_remove_woo_assets', 99 );
function my_remove_woo_assets() {
if ( function_exists( 'is_woocommerce' ) ) { // Check if Woo is installed.
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) { // Only run on non-Woo pages.
// Remove unnecessary stylesheets.
wp_dequeue_style( 'woocommerce-general' );
wp_dequeue_style( 'woocommerce-layout' );
wp_dequeue_style( 'woocommerce-smallscreen' );
wp_dequeue_style( 'woocommerce_frontend_styles' );
wp_dequeue_style( 'woocommerce_fancybox_styles' );
wp_dequeue_style( 'woocommerce_chosen_styles' );
wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
// Remove unnecessary scripts.
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' );
wp_dequeue_script( 'prettyPhoto' );
wp_dequeue_script( 'prettyPhoto-init' );
wp_dequeue_script( 'jquery-blockui' );
wp_dequeue_script( 'jquery-placeholder' );
wp_dequeue_script( 'fancybox' );
wp_dequeue_script( 'jqueryui' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment