/** | |
* Optimize WooCommerce Scripts | |
* Remove WooCommerce Generator tag, styles, and scripts from non WooCommerce pages. | |
*/ | |
add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 ); | |
function child_manage_woocommerce_styles() { | |
//remove generator meta tag | |
remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) ); | |
//first check that woo exists to prevent fatal errors | |
if ( function_exists( 'is_woocommerce' ) ) { | |
//dequeue scripts and styles | |
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) { | |
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_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' ); | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
After adding this code I still see 3 CSS scripts loaded... |
This comment has been minimized.
This comment has been minimized.
@DevinWalker Thank you. Do you add this to functions.php? |
This comment has been minimized.
This comment has been minimized.
@DevinWalker nevermind, I found your full article. Very nice write-up. |
This comment has been minimized.
This comment has been minimized.
I guess, this code needs some updating as it doesnt remove everything with the latest version of WooCommerce. |
This comment has been minimized.
This comment has been minimized.
IF you are using latest version of Woo, you need to add these three lines to remove ALL styles
|
This comment has been minimized.
This comment has been minimized.
Or you can remove all styles at once
|
This comment has been minimized.
This comment has been minimized.
this doesnt seem to work for site working on - https://officialicloudunlock.co.uk ? any ideas? |
This comment has been minimized.
This comment has been minimized.
Thanks @cnasikas, CSS are good to remove :) I try remove only few JS rows but not works. |
This comment has been minimized.
This comment has been minimized.
Here's what worked for me. Removes all styles & scripts. 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);
} |
This comment has been minimized.
This comment has been minimized.
Following worked for me to dequeue both styles and scripts. While example above by @jaswsinc didn't. add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_styles_scripts', 99 );
function dequeue_woocommerce_styles_scripts() {
if ( function_exists( 'is_woocommerce' ) ) {
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
# 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' );
# 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' );
}
}
} |
This comment has been minimized.
This comment has been minimized.
Hi @DevinWalker I apply your code, thanks for sharing, indeed, woocommerce is faster! does your code remove the ajax function from the "add to cart" button ? Because I use a plugin (woo popup) to create an animation when we add a product to the cart, this plugin uses ajax (otherwise it does not work), my problem is that if I use your code, my plugin Do you have a solution to let ajax activate ? |
This comment has been minimized.
This comment has been minimized.
up |
This comment has been minimized.
This comment has been minimized.
Good info - any pointers on how to modify the above code so that the woocommerce geolocation functionality doesn't get called for a given page? We need this on our site's store pages, but it's extremely slow and so we'd of course like to avoid calling it on all of the non-store pages. For clarity, I'm talking about the "geolocate with page caching support", that results in URLs with parameters like ?v=11aedd0e4327 appended. Thanks in advance for your help! |
This comment has been minimized.
This comment has been minimized.
Big props to @jaswrks, you saved me from rooting through api documentation. All scripts 100% gone, 3 lines of code.
|
This comment has been minimized.
This comment has been minimized.
Oh god! that was dificult! Thanks @alenabdula . Your code inside functions.php works perfectly ;) |
This comment has been minimized.
This comment has been minimized.
@RiFi2k |
This comment has been minimized.
This comment has been minimized.
It's not working anymore. Any tips? |
This comment has been minimized.
This comment has been minimized.
@DevinWalker @jaswrks @Basilou38 P.S: Still relevant today. add_filter('woocommerce_enqueue_styles', '__return_empty_array'); for CSS in 1 line pretty much.. it's in the Theming Docs. Side Note:
function gist_rm_wc_scripts() {
if (function_exists('is_woocommerce') {
# wp_dequeue above here
}
}
add_action('wp_enqueue_scripts', 'gist_rm_wc_scripts'); |
This comment has been minimized.
This comment has been minimized.
Hey guys, I have disable WooCommerce styles and scripts sitewide as I need good performance and it was killing my site performance. I have an issue now where the login form and other account forms have no styling. How can I target the account pages of members so that it displays the styling only for any user account related pages? |
This comment has been minimized.
I've removed the opening PHP tag so people won't be confused about that.