Skip to content

Instantly share code, notes, and snippets.

@katsar0v
Created July 25, 2021 13:58
Show Gist options
  • Save katsar0v/1e2b63265f2fd77dff1c13c7f7366c1f to your computer and use it in GitHub Desktop.
Save katsar0v/1e2b63265f2fd77dff1c13c7f7366c1f to your computer and use it in GitHub Desktop.
Optimize WooCommerce For Web Vitals
<?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