【问题标题】:WooCommerce 'remove_action' not working in 'init' hookWooCommerce 'remove_action' 在 'init' 钩子中不起作用
【发布时间】:2015-08-20 15:43:22
【问题描述】:

我正在尝试修改 WooCommerce 产品过滤器插件以将过滤器添加为 <div class="row"> 而不是 "col-md-6"

functions.php 中,我删除了与创建 col-md-6 的函数挂钩的操作,编写了创建 rows 的新函数,并添加了与我的新函数挂钩的操作。请参阅下面的代码。

function filter_styling()
{
    echo '<div class="row">';             
} 

function filter_styling2()
{
    echo '</div><div class="row">';            
}

function product_category_filter_changes()
{
    remove_action('woocommerce_before_main_content','oxy_before_breadcrumbs', 19);
    remove_action('woocommerce_before_main_content', 'oxy_after_breadcrumbs', 20);
    add_action('woocommerce_before_main_content', 'filter_styling', 19);  
    add_action('woocommerce_before_main_content', 'filter_styling2', 20);
}
add_action('init','product_category_filter_changes',10);

添加操作正在注册,而不是删除操作。有什么想法吗?

谢谢! 丹

【问题讨论】:

  • init 挂钩可能为时过早,无法删除操作。
  • 如果是这样,我会有什么不同的做法.....改变优先级?
  • 你可以试试把init改成template_redirect,去掉优先级...
  • 成功了!谢谢!!

标签: php wordpress woocommerce


【解决方案1】:

只有在已添加操作时才能删除操作。因此,'init' 挂钩可能为时过早。

我推荐使用'template_redirect' 动作钩子,它会在插件加载后运行:

function product_category_filter_changes()
{
    remove_action('woocommerce_before_main_content','oxy_before_breadcrumbs', 19);
    remove_action('woocommerce_before_main_content', 'oxy_after_breadcrumbs', 20);
    add_action('woocommerce_before_main_content', 'filter_styling', 19);  
    add_action('woocommerce_before_main_content', 'filter_styling2', 20);
}
add_action('template_redirect','product_category_filter_changes');

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2014-10-19
    • 2015-01-06
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多