【问题标题】:Remove JS file from Woocommerce Product edit pages从 Woocommerce 产品编辑页面中删除 JS 文件
【发布时间】:2017-08-05 05:57:41
【问题描述】:

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

实际上有 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');
}

谁能给我一个解决方案?

【问题讨论】:

    标签: wordpress woocommerce jquery-select2 wordpress-hook


    【解决方案1】:

    这不起作用,因为您使用了错误的操作。 在这里查看动作挂钩的正确用法: Hooks in Wordpress

    您将 admin_enqueue_scripts 操作挂钩放在 admin_init 操作挂钩内。 尝试将 admin_enqueue_scripts 放在 admin_init 钩子之外,如下所示:

    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');
        }
    
    }
    

    【讨论】:

    • 我做到了,但没有运气。
    • 出了什么问题?
    • 我是按我的方式做的 :)。从“WR PageBuilder”插件中删除了 js 文件,然后再次从 function.php 中为所有页面(excpet 产品页面)挂钩。现在它工作正常:)
    • 酷。您应该在其中添加答案。
    猜你喜欢
    • 2021-05-14
    • 2016-11-22
    • 1970-01-01
    • 2016-11-19
    • 2014-04-06
    • 2018-12-11
    • 2019-02-11
    • 2022-10-13
    • 2017-01-15
    相关资源
    最近更新 更多