无法使用 remove_action() 从 wordpress WooCommerce 挂钩中删除功能 [英] Can't remove function from wordpress WooCommerce hook using remove_action()

查看:21
本文介绍了无法使用 remove_action() 从 wordpress WooCommerce 挂钩中删除功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 WooCommerce 构建电子商务网站.我试图通过从默认挂钩中删除功能并将它们插入另一个挂钩来修改主题布局(因此它们出现在页面的不同部分.

我已经成功删除了一个函数并将其插入另一个钩子...

remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);add_action('woocommerce_below_single_product_display', 'woocommerce_template_single_meta', 40);

那个有效的发现,除了我不明白的是我必须使用优先级标签 40,更高的它不会工作,更少的它不会工作?我认为删除操作优先级必须是一个更高的数字,以便在创建操作后调用它.

现在页面上的一个元素我无法弄清楚它是如何插入到主题中的,使用另一个绅士函数列出了我能够隔离它的所有钩子和函数......

function list_hooked_functions($tag='woocommerce_single_product_summary'){全局 $wp_filter;如果($标签){$hook[$tag]=$wp_filter[$tag];如果 (!is_array($hook[$tag])) {trigger_error("没有找到'$tag'钩子", E_USER_WARNING);返回;}}别的 {$hook=$wp_filter;ksort($hook);}echo '

';foreach($hook as $tag => $priority){echo "<br/>>&gt;&gt;>&gt;\t<strong>$tag</strong><br/>";ksort($priority);foreach($priority as $priority => $function){回声 $priority;foreach($function as $name => $properties) echo "\t$name
";}}echo '</pre>';返回;}

这个函数返回的是...

我隔离了需要从钩子中删除的函数.所以我在我的functions.php中有这个

add_action('init', function(){remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);remove_action('woocommerce_single_product_summary', 'venedor_single_product_links', 3135);add_action('woocommerce_below_single_product_display', 'woocommerce_template_single_meta', 40);add_action('woocommerce_below_single_product_display', 'venedor_single_product_links', 40);}, 9000);

我只是无法删除 venedor_single_product_links 功能.我试过提高优先级数字,降低它,没有它.改用wp_head"操作.

任何人都可以建议为什么这可能不起作用以及我可以尝试的其他任何事情...

感谢您的时间.

解决方案

在您运行 remove_action 后,将添加venedor_single_product_links"操作.

我的建议是找出添加此操作的位置.

您在删除操作时使用的优​​先级必须与添加时相同.如果动作是用 40 添加的,正如您已经发现的,它必须用 40 删除.

动作和过滤器存储在一个数组中.数组的格式为['hook']['priority']['function'].这允许在具有不同优先级的单个挂钩上多次使用相同的操作.以下是 remove_filter 的相关片段(与 remove_action 相同):

unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ]

当您运行 remove_action 时,您正在从全局过滤器数组中取消设置该函数.如果您没有指定正确的优先级,您将尝试取消设置不存在的数组元素.

这并不意味着您替换它的任何操作都必须具有 40 的优先级.add_action 上的优先级可以是任何您喜欢的.

Building an ecommerce site with WooCommerce. I am trying to modify the theme layout by removing functions from the default hook, and plugging them into another one (so they appear on a different part of the page.

I've successfully removed one function and plugged it into another hook...

remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40); 
add_action('woocommerce_below_single_product_display', 'woocommerce_template_single_meta', 40);

That worked find, except what I don't understand is that I have to use the priority tag 40, any higher it won't work, and less it won't work? I thought the remove action priority just had to be a higher number so that it is called after the action is created.

Now one element on the page I couldn't figure out how it was getting inserted onto the theme, using another gentlemen's function that lists all hooks and functions I was able to isolate it...

function list_hooked_functions($tag='woocommerce_single_product_summary'){
     global $wp_filter;
     if ($tag) {
      $hook[$tag]=$wp_filter[$tag];
      if (!is_array($hook[$tag])) {
      trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
      return;
      }
     }
     else {
      $hook=$wp_filter;
      ksort($hook);
     }
     echo '<pre>';
     foreach($hook as $tag => $priority){
      echo "<br />&gt;&gt;&gt;&gt;&gt;\t<strong>$tag</strong><br />";
      ksort($priority);
      foreach($priority as $priority => $function){
      echo $priority;
      foreach($function as $name => $properties) echo "\t$name<br />";
      }
     }
     echo '</pre>';
     return;
}

What this function returned was...

I isolated the function that needed to be removed from the hook. So I have this in my functions.php

add_action('init', function(){

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40); 
    remove_action('woocommerce_single_product_summary', 'venedor_single_product_links', 3135);

    add_action('woocommerce_below_single_product_display', 'woocommerce_template_single_meta', 40); 
    add_action('woocommerce_below_single_product_display', 'venedor_single_product_links', 40);
}, 9000);   

I just can't get it to remove that venedor_single_product_links fucntions. I've tried raising the priority number, lowering it, not having it. Using the 'wp_head' action instead.

Can anyone suggest why this may not be working and anything else I can try...

Thanks for your time.

解决方案

The 'venedor_single_product_links' action is being added after you run remove_action.

My suggestion would be to find out where this action is being added.

The priority you use when removing an action has to be the same as it was added with. If the action was added with 40, as you've already discovered, it has to be removed with 40.

Actions and filters are stored in an array. The format of the array is ['hook']['priority']['function']. This allows multiple uses of the same action on a single hook with different priorities. Here's a relevant snippet from remove_filter (same as remove_action):

unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ]

When you run remove_action you're unsetting the function from the global filters array. If you don't specify the correct priority you'll be attempting to unset a non-existent array element.

That doesn't mean whatever action you replace it with must have a priority of 40. The priority on add_action can be anything you like.

这篇关于无法使用 remove_action() 从 wordpress WooCommerce 挂钩中删除功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆