Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active March 5, 2021 02:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilgee/487b7389662de5411f05 to your computer and use it in GitHub Desktop.
Save neilgee/487b7389662de5411f05 to your computer and use it in GitHub Desktop.
WooCommerce Conditionally Removing Scripts and Styles
<?php
add_action( 'template_redirect', 'bt_remove_woocommerce_styles_scripts', 999 );
/**
* Remove Woo Styles and Scripts from non-Woo Pages
* @link https://gist.github.com/DevinWalker/7621777#gistcomment-1980453
* @since 1.7.0
*/
function bt_remove_woocommerce_styles_scripts() {
// Skip Woo Pages
if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) {
return;
}
// Otherwise...
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);
}
<?php // <-don't add me
add_action('wp_enqueue_scripts','wpb_load_woocommerce');
function wpb_load_woocommerce() {
if( is_page(array( 'shop', 'cart', 'checkout','my-account' ) ) or 'product' == get_post_type() ) {
wp_enqueue_style( 'wpb-woo', get_stylesheet_directory_uri() . '/css/woocommerce.css', '', '3', 'all');
}
}
<?php // <-don't add me
add_action( 'wp_enqueue_scripts', 'wp_enqueue_woocommerce_style' );
function wp_enqueue_woocommerce_style(){
wp_register_style( 'mytheme-woocommerce', get_stylesheet_directory_uri() . '/css/woocommerce.css' );
if ( class_exists( 'woocommerce' ) ) {
wp_enqueue_style( 'mytheme-woocommerce' );
}
}
<?php // <-don't add me
// Remove each style one by one
add_filter( 'woocommerce_enqueue_styles', 'jk_dequeue_styles' );
function jk_dequeue_styles( $enqueue_styles ) {
unset( $enqueue_styles['woocommerce-general'] ); // Remove the gloss
unset( $enqueue_styles['woocommerce-layout'] ); // Remove the layout
unset( $enqueue_styles['woocommerce-smallscreen'] ); // Remove the smallscreen optimisation
return $enqueue_styles;
}
// Or just remove them all in one line
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment