Skip to content

Instantly share code, notes, and snippets.

@JiveDig
Last active March 22, 2021 11:15
Show Gist options
  • Save JiveDig/1466a6bb4b13bd80ab36f7ba4076a55b to your computer and use it in GitHub Desktop.
Save JiveDig/1466a6bb4b13bd80ab36f7ba4076a55b to your computer and use it in GitHub Desktop.
Remove WooCommerce scripts and styles.
<?php
/**
* Remove WooCommerce scripts and styles.
*
* Cart fragments are used for things like ajax cart menu icons,
* but it's really slow, so let's only keep them where necessary.
*
* @version 1.3.0
* @author Mike Hemberger @JiveDig
* @link https://gist.github.com/JiveDig/1466a6bb4b13bd80ab36f7ba4076a55b
*
* @return void
*/
add_action( 'wp_enqueue_scripts', function() {
// Bail if WooCommerce is not active.
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
// Bail if on WooCommerce pages.
if ( is_shop() || is_product() || is_cart() || is_checkout() || is_account_page() ) {
return;
}
// Remove the generator tag.
remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
$scripts = [
'accounting',
'wc-add-payment-method',
'wc-lost-password',
'wc-price-slider',
'wc-single-product',
'wc-add-to-cart',
'wc-cart-fragments',
'wc-credit-card-form',
'wc-checkout',
'wc-add-to-cart-variation',
'wc-single-product',
'wc-cart',
'wc-chosen',
'woocommerce',
// 'prettyPhoto',
// 'prettyPhoto-init',
// 'jquery-blockui',
// 'jquery-placeholder',
// 'jquery-payment',
// 'fancybox',
// 'jqueryui',
];
$styles = [
'woocommerce_frontend_styles',
'woocommerce-general',
'woocommerce-inline',
'woocommerce-layout',
'woocommerce-smallscreen',
'woocommerce_fancybox_styles',
'woocommerce_chosen_styles',
'woocommerce_prettyPhoto_css',
'wc-block-vendors-style',
'wc-block-style',
'printful-global', // printful-shipping-for-woocommerce
'wcsatt-css', // woocommerce-all-products-for-subscriptins
'wc-gateway-ppec-frontend', // woocommerce-gateway-paypal-express-checkout
'wc-memberships-frontend', // woocommerce-memberships
// 'select2',
];
foreach ( $scripts as $handle ) {
wp_dequeue_script( $handle );
}
foreach ( $styles as $handle ) {
wp_dequeue_style( $handle );
}
}, 999 );
@Elwood-P
Copy link

Elwood-P commented Mar 19, 2021

Tx very useful. You missed a few scripts:
'zoom', 'flexslider', 'photoswipe-ui-default', 'photoswipe',

and one style:
'photoswipe',

@JiveDig
Copy link
Author

JiveDig commented Mar 20, 2021

Interesting, in my testing with Query Monitor I didn’t see those loading on other pages. Are you seeing them loaded on your site(s)? Maybe it’s related to some features or add-ons I haven’t used?

@Elwood-P
Copy link

I should have been clearer, I am removing WC scripts & styles from all pages including WC pages (so much junk!), so these assets were being loaded on a product page. They get loaded by default on themes that havn't declared support for WC using add_theme_support('woocommerce');.

Thanks for the reference to Query Monitor by the way. I have no idea why I havn't come across this before, it looks so useful!

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