henrywright
henrywright

Reputation: 10240

How to remove a WordPress action which uses the current object - $this?

I'm familiar with remove_action when removing an action in WordPress.

To create the action: add_action( 'action_hook', 'function_name', 10, 3 );

To remove the action: remove_action( 'action_hook', 'function_name', 10, 3 );

But how can I remove an action which uses the current object? e.g $this

add_action( 'some_action_hook', array( $this, 'some_function' ) );

Ref:

http://codex.wordpress.org/Function_Reference/add_action

http://codex.wordpress.org/Function_Reference/remove_action

Upvotes: 5

Views: 5038

Answers (3)

Chris Trynkiewicz
Chris Trynkiewicz

Reputation: 336

Using the class name as suggested by the accepted answer didn't work for me in WordPress 4.5.

You can use these code snippets to remove the action (don't mind it being called "filter" on the project's page) by passing the method name or (a bit safer) the class name and method name: https://github.com/herewithme/wp-filters-extras/

Upvotes: 1

RavanH
RavanH

Reputation: 831

To extend on Rikesh answer: Sadly, using class name will not work. As it says on https://codex.wordpress.org/Function_Reference/remove_action

If an action has been added from within a class, for example by a plugin, removing it will require accessing the class variable.

This leaves only:

global $my_class;
remove_action( 'some_action_hook', array( $my_class, 'some_function' ) );

Or in case of a singleton class like Jetpack to remove the 'show_development_mode_notice' hook (for example) like this:

remove_action( 'jetpack_notices', array( Jetpack::init(), 'show_development_mode_notice' ) );

Upvotes: 4

Rikesh
Rikesh

Reputation: 26451

Inside class

add_action( 'some_action_hook', array( $this, 'some_function' ) );

Outside class,

With use of global vairable:

global $my_class;
remove_action( 'some_action_hook', array( $my_class, 'some_function' ) );

Using class name:

remove_action( 'some_action_hook', array( 'MyClass', 'some_function' ) );

Reference.

Upvotes: 5

Related Questions