butlerblog
8/3/2016 - 10:34 AM

This function removes select2 JS and CSS of WooCommerce so that theme can use its own.

This function removes select2 JS and CSS of WooCommerce so that theme can use its own.

<?php
if ( ! function_exists( 'remove_select2_wc' ) ) {

	/**
	 * remove_select2_wc.
	 *
	 * This function removes select2 JS and CSS so that 
	 * theme can use its own.
	 */
	function remove_select2_wc() {
		if ( class_exists( 'woocommerce' ) ) {
			wp_dequeue_style( 'select2' );
			wp_deregister_style( 'select2' );

			wp_dequeue_style( 'your-theme-main-css-file' );
			wp_deregister_style( 'your-theme-main-css-file' );

			wp_dequeue_script( 'select2');
			wp_deregister_script('select2');

			wp_dequeue_script( 'your-theme-js-file');
			wp_deregister_script( 'your-theme-js-file' );

			// select2 CSS
            wp_enqueue_style(
                'select2',
                get_template_directory_uri() . 'path-to-select2-css',
                array(),
                '1.0.0'
            );

            // main styles
            wp_enqueue_style(
                'your-theme-main-css-file',
                get_template_directory_uri() . 'path-to-your-theme-main-css-file',
                array( 'select2' ),
                1.0.0
            );

			// select2 JS
            wp_enqueue_script(
                'select2',
                get_template_directory_uri() . 'path-to-select2-js',
                array( 'jquery' ),
                '1.0.0',
                true
            );

            // Main js
            wp_enqueue_script(
                'your-theme-js-file',
                get_template_directory_uri() . 'path-to-your-theme-js-file',
                array( 'jquery', 'select2' ),
                1.0.0,
                true
            );
		} 
	} 

	add_action( 'wp_enqueue_scripts', 'remove_select2_wc', 100 );
}