[Support request] Removing Unused CSS

Home Forums Support [Support request] Removing Unused CSS

Home Forums Support Removing Unused CSS

  • This topic has 7 replies, 5 voices, and was last updated 1 year ago by Leo.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1679745
    Amir

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

    Thanks 🙂

    #1679773
    Elvin
    Staff
    Customer Support

    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/

    A wise man once said:
    "Have you cleared your cache?"

    #1679854
    Amir

    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’>

    #1680613
    Tom
    Lead Developer
    Lead Developer

    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.

    #1680842
    Amir

    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?

    #1681055
    Elvin
    Staff
    Customer Support

    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 );

    A wise man once said:
    "Have you cleared your cache?"

    #1930557
    Milan

    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' );
            }
        }
    }
    #1930558
    Leo
    Staff
    Customer Support

    Thanks so much for sharing!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.