Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Created March 25, 2024 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Acephalia/e93a7502536b2f04c7522d2efbf5834c to your computer and use it in GitHub Desktop.
Save Acephalia/e93a7502536b2f04c7522d2efbf5834c to your computer and use it in GitHub Desktop.
Woocommerce Dequeue Unused Styles & Scripts
add_action( 'template_redirect', 'remove_woocommerce_styles_scripts', 999 );
function remove_woocommerce_styles_scripts() {
if ( function_exists( 'is_woocommerce' ) ) {
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
remove_action('wp_enqueue_scripts', [WC_Frontend_Scripts::class, 'load_scripts']);
remove_action('wp_print_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
remove_action('wp_print_footer_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
}
}
}
function disable_wp_blocks() {
$wstyles = array(
'wp-block-library',
'wc-blocks-style',
'wc-blocks-style-active-filters',
'wc-blocks-style-add-to-cart-form',
'wc-blocks-packages-style',
'wc-blocks-style-all-products',
'wc-blocks-style-all-reviews',
'wc-blocks-style-attribute-filter',
'wc-blocks-style-breadcrumbs',
'wc-blocks-style-catalog-sorting',
'wc-blocks-style-customer-account',
'wc-blocks-style-featured-category',
'wc-blocks-style-featured-product',
'wc-blocks-style-mini-cart',
'wc-blocks-style-price-filter',
'wc-blocks-style-product-add-to-cart',
'wc-blocks-style-product-button',
'wc-blocks-style-product-categories',
'wc-blocks-style-product-image',
'wc-blocks-style-product-image-gallery',
'wc-blocks-style-product-query',
'wc-blocks-style-product-results-count',
'wc-blocks-style-product-reviews',
'wc-blocks-style-product-sale-badge',
'wc-blocks-style-product-search',
'wc-blocks-style-product-sku',
'wc-blocks-style-product-stock-indicator',
'wc-blocks-style-product-summary',
'wc-blocks-style-product-title',
'wc-blocks-style-rating-filter',
'wc-blocks-style-reviews-by-category',
'wc-blocks-style-reviews-by-product',
'wc-blocks-style-product-details',
'wc-blocks-style-single-product',
'wc-blocks-style-stock-filter',
'wc-blocks-style-cart',
'wc-blocks-style-checkout',
'wc-blocks-style-mini-cart-contents',
'classic-theme-styles-inline'
);
foreach ( $wstyles as $wstyle ) {
wp_deregister_style( $wstyle );
}
$wscripts = array(
'wc-blocks-middleware',
'wc-blocks-data-store'
);
foreach ( $wscripts as $wscript ) {
wp_deregister_script( $wscript );
}
}
add_action( 'init', 'disable_wp_blocks', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment