Forked from chris-castillo-dev/dequeue-wc-and-wp-scripts-or-styles.php
Created
November 27, 2024 06:38
-
-
Save louson21/b99a93b9e2dc73af964158a53360f004 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Removes WooCommerce scripts and styles from non-WooCommerce pages, | |
* excluding specific pages by post ID. | |
*/ | |
add_action('wp_print_scripts', 'my_remove_woo_assets', 999); | |
add_action('wp_enqueue_scripts', 'my_remove_woo_assets', 999); | |
function my_remove_woo_assets() { | |
// Array of post IDs to exclude from script removal | |
$excluded_pages = array( | |
123, // Some page | |
456 // Another page | |
); | |
// Get current post ID | |
$current_post_id = get_queried_object_id(); | |
if (function_exists('is_woocommerce')) { | |
// Only run on non-Woo pages and non-excluded pages | |
if (!is_woocommerce() && | |
!is_cart() && | |
!is_checkout() && | |
!is_account_page() && | |
!is_product_category() && | |
!in_array($current_post_id, $excluded_pages)) { | |
$style_handles = array( | |
'woocommerce-general', | |
'woocommerce-layout', | |
'woocommerce-smallscreen', | |
'woocommerce_frontend_styles', | |
'woocommerce_fancybox_styles', | |
'woocommerce_chosen_styles', | |
'woocommerce_prettyPhoto_css', | |
'wc-bookings-styles', | |
'wp-block-library', | |
'wc-blocks-style', | |
'wc-blocks-style-active-filters', | |
'wc-blocks-style-add-to-cart-form', | |
'wc-blocks-packages-style', | |
'wc-blocks-style-all-products', | |
'wc-blocks-style-all-reviews', | |
'wc-blocks-style-attribute-filter', | |
'wc-blocks-style-breadcrumbs', | |
'wc-blocks-style-catalog-sorting', | |
'wc-blocks-style-customer-account', | |
'wc-blocks-style-featured-category', | |
'wc-blocks-style-featured-product', | |
'wc-blocks-style-mini-cart', | |
'wc-blocks-style-price-filter', | |
'wc-blocks-style-product-add-to-cart', | |
'wc-blocks-style-product-button', | |
'wc-blocks-style-product-categories', | |
'wc-blocks-style-product-image', | |
'wc-blocks-style-product-image-gallery', | |
'wc-blocks-style-product-query', | |
'wc-blocks-style-product-results-count', | |
'wc-blocks-style-product-reviews', | |
'wc-blocks-style-product-sale-badge', | |
'wc-blocks-style-product-search', | |
'wc-blocks-style-product-sku', | |
'wc-blocks-style-product-stock-indicator', | |
'wc-blocks-style-product-summary', | |
'wc-blocks-style-product-title', | |
'wc-blocks-style-rating-filter', | |
'wc-blocks-style-reviews-by-category', | |
'wc-blocks-style-reviews-by-product', | |
'wc-blocks-style-product-details', | |
'wc-blocks-style-single-product', | |
'wc-blocks-style-stock-filter', | |
'wc-blocks-style-cart', | |
'wc-blocks-style-checkout', | |
'wc-blocks-style-mini-cart-contents', | |
'classic-theme-styles-inline', | |
'bricks-woocommerce', | |
'some-stylesheet', // Any other handles | |
'some-other-stylesheet' // Go here | |
); | |
// Remove scripts | |
$scripts_to_remove = array( | |
'bricks-woocommerce', | |
'sourcebuster-js', | |
'wc-order-attribution', | |
'wc_price_slider', | |
'wc-single-product', | |
'wc-add-to-cart', | |
'wc-cart-fragments', | |
'wc-checkout', | |
'wc-add-to-cart-variation', | |
'wc-single-product', | |
'wc-cart', | |
'wc-chosen', | |
'woocommerce', | |
'wc-blocks-middleware', | |
'wc-blocks-data-store', | |
'prettyPhoto', | |
'prettyPhoto-init', | |
'jquery-blockui', | |
'jquery-placeholder', | |
'fancybox', | |
'jqueryui', | |
'some-js-script', // More js handles | |
'some-other-js-script' // Go here | |
); | |
foreach ( $style_handles as $style_handle ) { | |
wp_deregister_style( $style_handle ); | |
} | |
foreach ($scripts_to_remove as $script) { | |
wp_dequeue_script($script); | |
wp_deregister_script($script); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment