Josh Regev
Josh Regev

Reputation: 379

Wordpress - remove_action from plugin class

In WordPress, how can I remove the action below, which is inside a class in a plugin?

The plugin includes this (I'm just quoting a few lines of it):

class WC_Product_Vendors_Vendor_Admin {
    public static function init() {
        self::$self = new self();
        add_action( 'pre_get_users', array( self::$self, 'filter_users' ) );
    }
}
WC_Product_Vendors_Vendor_Admin::init();

In my own custom plugin, I tried:

function remove_my_class_action() {
    remove_action ( 'pre_get_users', array( WC_Product_Vendors_Vendor_Admin::$instance , 'filter_users') );
}
add_action( 'plugins_loaded', 'remove_my_class_action' );

But Wordpress doesn't even let me save it — it says the class is not found.

I also tried getInstance() instead of $instance.

Instead of 'plugins_loaded' I also tried 'wp_head' and 'init'.

Also tried different priorities on both add_action and remove_action, it doesn't make a difference. (Though it could be there's some combination of the above that I missed.)

Upvotes: 0

Views: 820

Answers (1)

Davide Carlier
Davide Carlier

Reputation: 129

Try to use the same hook with higher priority (default is 10). Also the static property $instance doesn't exist on that class. In the code you paste from the plugin you can see that in the init function it uses the $self static property to store the instance.

function remove_my_class_action() {
    remove_action ( 'pre_get_users', array( WC_Product_Vendors_Vendor_Admin::$self , 'filter_users') );
}
add_action( 'pre_get_users', 'remove_my_class_action', 9 );

Upvotes: 1

Related Questions