Skip to content

Instantly share code, notes, and snippets.

@jameshwartlopez
Last active June 22, 2020 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshwartlopez/5f80e6f99b6983a680f6e4de0910d84f to your computer and use it in GitHub Desktop.
Save jameshwartlopez/5f80e6f99b6983a680f6e4de0910d84f to your computer and use it in GitHub Desktop.
Unload unneccessary woocommerce js and css
add_action( 'wp_enqueue_scripts', function(){
//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' ) ) {
// only load cart.js & cart.cssto cart page
if(is_cart()){
}
// load anythin for checkout page only
if(is_checkout()) {
}
// load anything for account page only
if(is_account_page()){
}
// only load search.css on archive search
if(is_search()) {
}
// load anything for front page only
if(is_front_page()) {
}
//dequeue scripts and styles unless we are in store
// only load on cart, checkout and search
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_search()) {
// see https://gist.github.com/DevinWalker/7621777
wp_dequeue_style( 'woocommerce_frontend_styles' );
wp_dequeue_style( 'woocommerce_fancybox_styles' );
wp_dequeue_style( 'woocommerce_chosen_styles' );
wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
// see https://gist.github.com/gregrickaby/2846416
wp_dequeue_script( 'selectWoo' );
wp_deregister_script( 'selectWoo' );
wp_dequeue_script( 'wc-add-payment-method' );
wp_dequeue_script( 'wc-lost-password' );
wp_dequeue_script( 'wc_price_slider' );
wp_dequeue_script( 'wc-single-product' );
/*
Do not deque cart scripts since there is a global icon to show cart popup
*/
// wp_dequeue_script( 'wc-add-to-cart' );
// wp_dequeue_script( 'wc-cart-fragments' );
// wp_dequeue_script( 'wc-cart' );
wp_dequeue_script( 'wc-add-to-cart-variation' );
wp_dequeue_script( 'wc-credit-card-form' );
wp_dequeue_script( 'wc-checkout' );
wp_dequeue_script( 'wc-single-product' );
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( 'jquery-payment' );
wp_dequeue_script( 'fancybox' );
wp_dequeue_script( 'jqueryui' );
// see https://crunchify.com/how-to-stop-loading-woocommerce-js-javascript-and-css-files-on-all-wordpress-postspages/
wp_dequeue_style('woocommerce-layout');
wp_dequeue_style('woocommerce-general');
wp_dequeue_style('woocommerce-smallscreen');
wp_dequeue_style('wc-block-style-css'); // could not be removed???
}
}
}, 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment