从Woocommerce产品编辑页面中删除JS文件

时间:2017-08-05 05:57:41

标签: wordpress woocommerce jquery-select2 wordpress-hook

我收到此错误"未捕获错误:选项' ajax'附加到元素时不允许使用Select2。"同时更新产品变更。

实际上有2个select2.js文件,一个来自Woocommerce,另一个来自WR PageBuilder'插入。我正在重命名WR PageBuilder' select2.js文件然后它的工作正常。但编辑器需要该文件。

我想仅从产品页面删除该js文件。

我做了'wp_deregister_script()'和' wp_dequeue_script()'但什么都没发生。

这是我的代码:

add_action('admin_init', 'functon_to_filter_script');
function functon_to_filter_script() {
global $typenow;
// when editing pages, $typenow isn't set until later!
if (empty($typenow)) {
    // try to pick it up from the query string
    if (!empty($_GET['post'])) {
        $post = get_post($_GET['post']);
        $typenow = $post->post_type;
    }
}
if( 'product' == $typenow ){
    add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );

}
}
function deregister_my_script() {
  wp_dequeue_script('wr-pagebuilder');
  wp_deregister_script('wr-pagebuilder');
}

任何人都可以给我一个解决方案吗?

1 个答案:

答案 0 :(得分:0)

This won't work because you are using the actions wrong. Look here for the correct usage of action hooks: Hooks in Wordpress

You are putting the admin_enqueue_scripts action hook inside of the admin_init action hook. Try taking admin_enqueue_scripts outside of the admin_init hook like this:

global $typenow;

add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );


function deregister_my_script() {
    if (!empty($_GET['post'])) {
        $post = get_post($_GET['post']);
        $typenow = $post->post_type;
    }

    if( 'product' == $typenow ){
        wp_dequeue_script('wr-pagebuilder');
        wp_deregister_script('wr-pagebuilder');
    }

}