Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

wordpress

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

like image 273
henrywright Avatar asked Mar 03 '14 09:03

henrywright


People also ask

How do I remove a filter in WordPress?

WordPress is great for allowing developers to extend plugins and themes using action hooks and filters. Sometimes we need to remove an already declared action or filter in a plugin or theme. It's pretty easy to do this in functional programming using remove_action() and remove_filter().

How do I remove a class action in WordPress?

If you are a WordPress developer, then you must have definitely used action and filter hooks. You might have also known that add_action() hook can be removed using remove_action() & add_filter() hook can be removed using remove_filter() functions.

How do I remove a function in WordPress?

By setting a lower priority, the action will be executed earlier. By setting a higher priority, the action will be executed later. Whether it is an add_action or remove_action the principal is the same. 2] So set the priority of remove_action and add_action to the same number, in this case, 10.

How do I remove a hook in WordPress?

To remove a hook, the $callback and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.


1 Answers

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.

like image 106
Rikesh Avatar answered Oct 12 '22 12:10

Rikesh