Created
July 25, 2021 13:58
-
-
Save katsar0v/1e2b63265f2fd77dff1c13c7f7366c1f to your computer and use it in GitHub Desktop.
Optimize WooCommerce For Web Vitals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Disable WooCommerce block styles (front-end). | |
*/ | |
function slug_disable_woocommerce_block_styles() { | |
if( function_exists( 'is_woocommerce') ) { | |
if( !is_woocommerce() && !is_cart() && !is_checkout() ) { | |
wp_dequeue_style( 'wc-block-style' ); | |
wp_dequeue_style( 'wp-block-library' ); | |
wp_dequeue_style( 'woocommerce-layout' ); | |
wp_dequeue_style( 'woocommerce-smallscreen' ); | |
wp_dequeue_style( 'woocommerce-general' ); | |
wp_dequeue_style( 'photoswipe' ); | |
wp_dequeue_style( 'photoswipe-default-skin' ); | |
wp_dequeue_script( 'wc-add-to-cart' ); | |
wp_dequeue_script( 'photoswipe' ); | |
wp_dequeue_script( 'photoswipe-ui-default' ); | |
wp_dequeue_script( 'wc-cart-fragments' ); | |
} | |
} | |
} | |
add_action( 'wp_enqueue_scripts', 'slug_disable_woocommerce_block_styles', PHP_INT_MAX ); | |
add_filter( 'woocommerce_enqueue_styles', function($arr) { | |
if( function_exists( 'is_woocommerce') ) { | |
if( !is_woocommerce() && !is_cart() && !is_checkout() ) { | |
return []; | |
} | |
} | |
return $arr; | |
} ); | |
/** | |
* Remove lightbox | |
*/ | |
add_action( 'wp', function() { | |
remove_theme_support( 'wc-product-gallery-lightbox' ); // removes photoswipe markup on frontend | |
}, PHP_INT_MAX ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment