Skip to content

Instantly share code, notes, and snippets.

@ontiuk
Last active September 2, 2024 04:54
Show Gist options
  • Save ontiuk/72f6ac868d397678fb8b31df2b22e32a to your computer and use it in GitHub Desktop.
Save ontiuk/72f6ac868d397678fb8b31df2b22e32a to your computer and use it in GitHub Desktop.
Woocommerce Remove Select2 / SelectWoo
// Add to your theme's functions.php file. De-queues Select2 styles & scripts. Useful to keep Boostrap form control formatting
/**
* Remove Woocommerce Select2 - Pre WC 3.2.1-ish
*/
function woo_dequeue_select2() {
if ( class_exists( 'woocommerce' ) ) {
wp_dequeue_style( 'select2' );
wp_deregister_style( 'select2' );
wp_dequeue_script( 'select2');
wp_deregister_script('select2');
}
}
add_action( 'wp_enqueue_scripts', 'woo_dequeue_select2', 100 );
/**
* Remove Woocommerce Select2 - Woocommerce 3.2.1+
*/
function woo_dequeue_select2() {
if ( class_exists( 'woocommerce' ) ) {
wp_dequeue_style( 'select2' );
wp_deregister_style( 'select2' );
wp_dequeue_script( 'selectWoo');
wp_deregister_script('selectWoo');
}
}
add_action( 'wp_enqueue_scripts', 'woo_dequeue_select2', 100 );
@marypieroszkiewicz
Copy link

when disable select2 it stopped working quantity buttons plus/minus on product page how can i fix it ...?

@seostudio
Copy link

This is the stupidest solution ever seen. What if other plugins require select2?

@harellanov
Copy link

harellanov commented Aug 23, 2022

This is the stupidest solution ever seen. What if other plugins require select2?

It's not stupid. Is helpful. U can use this code for your custom code. In example:

// Add to your theme's functions.php file. 
// De-queues Select2 styles & scripts ONLY in the Woocommerce Checkout.
// Useful to keep Boostrap form control formatting

/**
 * Remove Woocommerce Select2 only for Checkout - Woocommerce 3.2.1+
 */
function woo_dequeue_select2_only_for_checkout() {

	if ( is_checkout() ) {
	    if ( class_exists( 'woocommerce' ) ) {
	        wp_dequeue_style( 'select2' );
	        wp_deregister_style( 'select2' );

	        wp_dequeue_script( 'selectWoo');
	        wp_deregister_script('selectWoo');
	    } 
	}
}
add_action( 'wp_enqueue_scripts', 'woo_dequeue_select2_only_for_checkout', 100 );

For me this was the best solution, simple and clean.

Thanks a lot @ontiuk

@gm-style
Copy link

gm-style commented Sep 1, 2022

This is the stupidest solution ever seen. What if other plugins require select2?

In my experience Select2 is the stupidest select decorator ever seen. Anyway a decent plugin should integrate a check to see if it exists, and if not run his own, so disabling it is not a problem at all.

@aschultzapiam
Copy link

I've seen sites that have had select-2 enqueued 4 times. Most of my builds these days are custom and I hardly ever use any plugins apart from ACF and WooCommerce. Too much of a headache trying to unpick things...

@sean-mystyle
Copy link

This is the stupidest solution ever seen. What if other plugins require select2?

Pretty simple, you check if they do, and make conditions to allow it load where needed, and otherwise you improve page load speed and rank by eliminating unnecessary render blocking resources. Congrats for the stupidest comment on this page.

@seostudio
Copy link

@sean-mystyle What if other plugins require selectWoo?

wp_enqueue_script ('custom_js', plugins_url ('js/custom.js', __FILE__), array ('selectWoo'));

@sean-mystyle
Copy link

@sean-mystyle What if other plugins require selectWoo?

wp_enqueue_script ('custom_js', plugins_url ('js/custom.js', __FILE__), array ('selectWoo'));

Instead of wondering "what if", just check if it's being used or not and know where it's safe to remove it or not. For example, this is for select boxes UI right? Well, if there's no select boxes on the page, then it's not being used and is adding bloat to the page for no reason.

Write some code that only dequeues it on pages that don't use it. For me the only pages that need this are product pages, cart, and checkout. So the code says is it one of those pages? If not, dequeue it, and it speeds up the page load time with one less render blocking resource.

@Neshable
Copy link

Neshable commented Sep 2, 2024

For newer versions of WooCommerce, the handle that should be removed is wc-country-select :

wp_dequeue_script( 'wc-country-select');
wp_deregister_script( 'wc-country-select' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment