如何卸载新WooCommerce 2.3.x加载的select2脚本/样式?

Dmi*_*try 5 javascript css php wordpress

我们是主题开发人员,我们已经在wordpress主题的HTML中使用select2(http://select2.github.io/)脚本.刚刚推出的新WooCommerce 2.3.x现在也使用select2脚本.但是它们以许多不同的方式用自己的(最重要的是!重要标签)覆盖它的样式.

我们不能重新声明他们所有的CSS更改!重要的规则,这将是我们不喜欢的很多CSS混乱.此外,现在我们的JS脚本加载了2次并且有时也会发生冲突(因为woocommerce加载了自己的select2 js).

我的问题是什么是在我们的主题函数中停用woocommerce select2 CSS和JS文件的最佳方法?我们想要使用我们的脚本版本和样式文件(对于woocommerce select元素).

我尝试使用wp_dequeue_script,wp_deregister_script以及可用于样式的相同函数,但这没有帮助.正如我看到woocommerce在我们的主题已经初始化之后添加脚本/ css,我们无法停用它.

谢谢.

这是我需要在主题函数中禁用的/includes/class-wc-frontend-scripts.php中加载的代码:

self::register_script( 'select2', $assets_path . 'js/select2/select2' . $suffix . '.js', array( 'jquery' ), '3.5.2' );
self::enqueue_script( 'select2' );
wp_enqueue_style( 'select2', $assets_path . 'css/select2.css' );
Run Code Online (Sandbox Code Playgroud)

如何在主题函数中卸载这个CSS/JS文件,而不更改原始的WooCommerce文件?

Dmi*_*try 23

我找到了解决方案:

add_action( 'wp_enqueue_scripts', 'mgt_dequeue_stylesandscripts', 100 );

function mgt_dequeue_stylesandscripts() {
    if ( class_exists( 'woocommerce' ) ) {
        wp_dequeue_style( 'select2' );
        wp_deregister_style( 'select2' );

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

    } 
} 
Run Code Online (Sandbox Code Playgroud)


And*_*kUp 6

WooCommerce(v 3.2.1)最近发生了一些变化,他们分叉了 select2 脚本,因此@Dmitry 上面的代码不再起作用。对我有用的新代码如下..

add_action( 'wp_enqueue_scripts', 'agentwp_dequeue_stylesandscripts', 100 );

function agentwp_dequeue_stylesandscripts() {
    if ( class_exists( 'woocommerce' ) ) {
    wp_dequeue_style( 'selectWoo' );
    wp_deregister_style( 'selectWoo' );

    wp_dequeue_script( 'selectWoo');
    wp_deregister_script('selectWoo');

   }
}
Run Code Online (Sandbox Code Playgroud)