Skip to content

Instantly share code, notes, and snippets.

@carasmo
Last active February 15, 2024 12:20
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carasmo/f8442d984058c6067fd3ec6d04eda227 to your computer and use it in GitHub Desktop.
Save carasmo/f8442d984058c6067fd3ec6d04eda227 to your computer and use it in GitHub Desktop.
Dequeue WooCommerce CSS and JS on non-WooCommerce pages for 3.6.4
<?php
//don't add above twice if it already exists
/**
* Check if WooCommerce Page of all kinds (not just cart and shop)
* @thanks: https://faish.al/2014/01/06/check-if-it-is-woocommerce-page/
* Checked IDS with WooCommerce Repo 1 June 2019
*/
function ca_is_really_woocommerce_page() {
if( function_exists ( 'is_woocommerce' ) && is_woocommerce() ) :
return true;
endif;
$woocommerce_keys = array (
'woocommerce_shop_page_id' ,
'woocommerce_terms_page_id' ,
'woocommerce_cart_page_id' ,
'woocommerce_checkout_page_id' ,
'woocommerce_pay_page_id' ,
'woocommerce_thanks_page_id' ,
'woocommerce_myaccount_page_id' ,
'woocommerce_edit_address_page_id' ,
'woocommerce_view_order_page_id' ,
'woocommerce_change_password_page_id' ,
'woocommerce_logout_page_id' ,
'woocommerce_lost_password_page_id' ) ;
foreach ( $woocommerce_keys as $wc_page_id ) :
if ( get_the_ID () == get_option ( $wc_page_id , 0 ) ) :
return true;
endif;
endforeach;
return false;
}
add_action( 'wp_enqueue_scripts', 'ca_dequeue_woocommerce_js_css_on_non_woo_pages', 99 );
/**
* Remove WooCommerce CSS and JS on non-WooCommerce pages for 3.6.4
* Keep this updated by looking here:
* https://github.com/woocommerce/woocommerce/blob/44d129e45286b782fcce2ba67acf837a46e0c6c9/includes/class-wc-frontend-scripts.php
*
* Comment out the ones you want to keep
* -- Remember that now any sidebar / widget / shortcode from WooCommerce used on a
* -- NON-Woo Page will not look good and won't work
*/
function ca_dequeue_woocommerce_js_css_on_non_woo_pages() {
remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
if ( ! function_exists( 'is_woocommerce' ) ) return; //exit if does not exist
// Dequeue if is not WooCommerce page and not cart and not checkout
// Add more conditions eg ` && ! is_front_page() ` to load on front page
if (
! ca_is_really_woocommerce_page()
) :
// dequeue styles
$dequeue_style = array(
'woocommerce-layout',
'wc-block-style',
'woocommerce-inline',
'woocommerce-smallscreen',
'woocommerce-general',
'select2',
);
foreach ( $dequeue_style as $dstyle ) :
wp_dequeue_style( $dstyle );
endforeach;
// dequeue scripts
$dequeue_script = array(
'flexslider',
'js-cookie',
'jquery-blockui',
'jquery-cookie', //deprecated
'jquery-payment',
'photoswipe' ,
'photoswipe-ui-default',
'prettyPhoto', //deprecated
'prettyPhoto-init', //deprecated
'select2',
'selectWoo',
'wc-address-i18n',
'wc-add-payment-method',
'wc-cart',
'wc-cart-fragments',
'wc-checkout',
'wc-country-select',
'wc-credit-card-form',
'wc-add-to-cart',
'wc-add-to-cart-variation',
'wc-geolocation',
'wc-lost-password',
'wc-password-strength-meter',
'wc-single-product',
'woocommerce',
'zoom',
);
foreach ( $dequeue_script as $dscript ) :
wp_dequeue_script( $dscript );
endforeach;
endif; // conditions met
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment