Can't remove function from wordpress WooCommer

2019-06-10 02:25发布

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...

function above returns this... 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.

1条回答
ら.Afraid
2楼-- · 2019-06-10 02:51

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.

查看更多
登录 后发表回答