How to remove an action that is added inside a class

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x