WooCommerce 'remove_action' 在 'init' 挂钩中不工作

WooCommerce 'remove_action' not working in 'init' hook

我正在尝试修改 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' 挂钩可能为时过早。

我推荐使用 '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');