Site logo

Archived topic

Removing Unused CSS

7 replies · Started by Amir on March 2, 2021

Viewing posts 1–8 of 8

Hey,
How can I remove the dequeue WooCommerce CSS file and inline-CSS from the non-WooCommerce pages?

Thanks :)

Hi there,

Try this PHP snippet:

//* Enqueue scripts and styles
add_action( 'wp_enqueue_scripts', 'crunchify_disable_woocommerce_loading_css_js' );
 
function crunchify_disable_woocommerce_loading_css_js() {
 
	// Check if WooCommerce plugin is active
	if( function_exists( 'is_woocommerce' ) ){
 
		// Check if it's any of WooCommerce page
		if(! is_woocommerce() && ! is_cart() && ! is_checkout() ) { 		
			
			## Dequeue WooCommerce styles
			wp_dequeue_style('woocommerce-layout'); 
			wp_dequeue_style('woocommerce-general'); 
			wp_dequeue_style('woocommerce-smallscreen'); 	
 
			## Dequeue WooCommerce scripts
			wp_dequeue_script('wc-cart-fragments');
			wp_dequeue_script('woocommerce'); 
			wp_dequeue_script('wc-add-to-cart'); 
		
			wp_deregister_script( 'js-cookie' );
			wp_dequeue_script( 'js-cookie' );
 
		}
	}	
}

Reference: https://crunchify.com/how-to-stop-loading-woocommerce-js-javascript-and-css-files-on-all-wordpress-postspages/

Thanks Elvin but I am asking about removing the CSS generated by GP Premium plugin.

/gp-premium/woocommerce/functions/css/woocommerce.min.css
/gp-premium/woocommerce/functions/css/woocommerce-mobile.min.css
<style id='generate-woocommerce-inline-css'>

Hi there,

Trouble with doing something like this is figuring out what is and isn't a WooCommerce page. Things like shortcodes and now blocks blur those lines considerably.

However, this is how you would remove those files:

add_action( 'wp_enqueue_scripts', function() {
    wp_dequeue_style( 'generate-woocommerce' );
    wp_dequeue_style( 'generate-woocommerce-mobile' );
}, 200 );

Then you'd need to figure out the right condition to wrap those functions in so it only applies to non-WooCommerce pages.

Thanks Tom!
In our case we are not using the product blocks/shortcodes anywhere other then on the usual WooCommerce theme/pages so I guess we can simply use this if(! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
with wp_dequeue_style?

You can try this

add_action( 'wp_enqueue_scripts', function() {
	// Check if WooCommerce plugin is active
	if( function_exists( 'is_woocommerce' ) ){
 
		// Check if it's any of WooCommerce page
	if(! is_woocommerce() && ! is_cart() && ! is_checkout() ) { 		
            wp_dequeue_style( 'generate-woocommerce' );
            wp_dequeue_style( 'generate-woocommerce-mobile' );
	}
	}
}, 200 );

In case someone googled this, here is the full working code as on 14/09/2021

add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_styles_scripts', 200 );
function dequeue_woocommerce_styles_scripts() {
	//remove generator meta tag
	remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
    if ( function_exists( 'is_woocommerce' ) ) {
        if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && !is_account_page()) {
            # Styles
            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' );
	    wp_dequeue_style( 'woocommerce-inline');
	    wp_dequeue_style( 'generate-woocommerce' );
            wp_dequeue_style( 'generate-woocommerce-mobile' );
            # 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' );
        }
    }
}

Thanks so much for sharing!

This archived topic is closed to new replies.