admin管理员组

文章数量:1134574

Here is the code in short that is coming from a plugin:

class Post_Views_Counter_Columns {
     public function __construct() {
        add_action( 'wp', array( $this, 'admin_bar_maybe_add_style' ) );
    }
}

I want to remove this admin_bar_maybe_add_style function. I am trying with following code in my child theme's functions.php

remove_action( 'wp', array( 'Post_Views_Counter_Columns', 'admin_bar_maybe_add_style' ) );

It's not working. Any suggestions?

Here is the code in short that is coming from a plugin:

class Post_Views_Counter_Columns {
     public function __construct() {
        add_action( 'wp', array( $this, 'admin_bar_maybe_add_style' ) );
    }
}

I want to remove this admin_bar_maybe_add_style function. I am trying with following code in my child theme's functions.php

remove_action( 'wp', array( 'Post_Views_Counter_Columns', 'admin_bar_maybe_add_style' ) );

It's not working. Any suggestions?

Share Improve this question edited Aug 5, 2021 at 12:10 Morshed Alam Sumon asked Aug 5, 2021 at 11:53 Morshed Alam SumonMorshed Alam Sumon 1571 gold badge2 silver badges7 bronze badges 5
  • Is that class coming from a plugin, or where does it inherit from? – WPTricksDK Commented Aug 5, 2021 at 12:04
  • @WPTricksDK it's coming from a plugin. Sorry I didn't mention that. – Morshed Alam Sumon Commented Aug 5, 2021 at 12:10
  • Please take a look at this thread: stackoverflow.com/questions/36639146/… Don't know if you've tried this already or not - but it's worth a shot. – WPTricksDK Commented Aug 5, 2021 at 12:25
  • Where/how is Post_Views_Counter_Columns instantiated? – Jacob Peattie Commented Aug 5, 2021 at 13:06
  • 2 I hate this type of classes, in order to remove the action you need to create a instance of that class, when creating an instance the action is called... so anoying. See Removing action added from constructor – Buttered_Toast Commented Aug 5, 2021 at 14:03
Add a comment  | 

2 Answers 2

Reset to default 2

I don't understand any of it but it seems to work just fine. Thank you @Buttered_Toast for mentioning the useful thread.

add_action("init", function() {
    global $wp_filter;
    foreach($wp_filter["wp"][10] as $id => $filter) {
    if(is_array($filter["function"]) && count($filter["function"]) == 2 &&
        get_class($filter["function"][0]) == "Post_Views_Counter_Columns" &&
        $filter["function"][1] == "admin_bar_maybe_add_style") {
            remove_action("wp", $id);
        }
    }
}, 99);

I also wrote my own little funciton, very similar to the one mentioned above. Here it is, it considers all priorities of a given hook or only one if you enter a numeric priority.

You can define your own check function.

    function dynamically_remove_hooks($hook, $priorities, $callback_check_function)
{
    global $wp_filter;
    if(!isset($wp_filter[$hook]))
        return;

    if(is_numeric($priorities))
        $priorities = array((int)$priorities);
    elseif(!$priorities && isset($wp_filter[$hook]->callbacks) && is_array($wp_filter[$hook]->callbacks))
        $priorities = array_keys($wp_filter[$hook]->callbacks);

    foreach($priorities as $priority)
    {
        if(isset($wp_filter[$hook]->callbacks[$priority]))
        {
            foreach ($wp_filter[$hook]->callbacks[$priority] as $identifier => $callback) {
                if ($callback_check_function( $callback )) {
                    unset( $wp_filter[$hook]->callbacks[$priority][$identifier] );
                }
            }
        }
    }
}

You call it like this, if e.g. you would like to remove all hooks created by the CLASS Post_Views_Counter_Columns for the hook name wp:

dynamically_remove_hooks('wp', false, function($callback) {
    return isset($callback['function'][0]) && get_class($callback['function'][0]) == 'Post_Views_Counter_Columns ';
});

You can define all sorts of callback check functions to have various different logics :)

本文标签: How to remove an action that is added inside a class