且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法使用 remove_action() 从 wordpress WooCommerce 挂钩中删除功能

更新时间:2023-11-30 13:46:52

在您运行 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.