5

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

henrywright
  • 10,070
  • 23
  • 89
  • 150

3 Answers3

5

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.

Community
  • 1
  • 1
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Hi Rikesh - thank you for this. Which approach would you recommend? Using the global var or using the class name? – henrywright Mar 03 '14 at 09:57
  • Using class name. [Read Why use of globals are bad](http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why). – Rikesh Mar 03 '14 at 10:16
4

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' ) );
RavanH
  • 791
  • 5
  • 5
1

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/

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/13284360) – eisbehr Aug 10 '16 at 06:24
  • Yes - if the code snippet is short. In this case, it's a plugin, so posting the whole source code and docs here doesn't seem to be a good idea. It's forked 11 times, so it won't vanish easily. Sometimes a link is the proper solution. – Chris Trynkiewicz Aug 10 '16 at 12:03
  • Nobody says that a link is bad. But there is no problem to add the a useage example of the plugin here too. You should not pase all source and examples here. – eisbehr Aug 10 '16 at 12:08
  • Okay, so why was my answer deleted when it's the only currently working answer? Because someone says that a link to a project is not enough, even though the project has clear usage examples on the front page? Seriously, think about your decisions before making such a call. You can downvote or comment all you want, but deleting a legit answer is not helping anyone. – Chris Trynkiewicz Aug 13 '16 at 19:59