Omer Omer - 1 year ago 92
PHP Question

How to remove_action if add_action is in abstract class in wordpress

I'm working on a WordPress plugin and I need to remove an action that the activated theme is adding.

I know I can do it simply adding

remove_action( $tag, $function_to_remove, $priority );
but that doesn't work for me.

Theme is adding
through an abstract class like this:

abstract class foo {

public function __construct() {

add_action('wp_edit_nav_menu_walker', array(&$this, 'wp_edit_nav_menu_walker_custom', 10));



And here is the subclass that is implementing the above abstract class:

class bar extends foo {

public function __contruct() {}


new bar();

And there is no
assigned to the instance of the subclass that implements this abstract class.

So I want to remove this action through my plugin but not sure how?

My plugin is also
based. So any help or suggestions will be appreciated

Answer Source

Ugh, that method of creating a class instance is completely wrong. Sometimes you can't just work with broken code in a reasonable manner. However, I've seen some authors do this while also implementing a getInstance() method.

If the class you are working with contains a method like that, you can do something like

remove_action( 'wp_edit_nav_menu_walker', array( bar::getInstance(), 'wp_edit_nav_menu_walker_custom' ), 10 );

If the theme author was completely without a clue, then there is still one last resort. There is a pair of functions that can be included in your code base to overcome this programming error. It can be found here...


Hmm, I just looked up the "wp_edit_nav_menu_walker" hook. It's a filter not an action. Your sample was a little misleading.

I believe it would be easier to override the filter with the default Walker Class like so. Make sure the priority is higher than the one in the theme. You will also need to modify the code below to work in your OOP plugin.

function restore_edit_walker( $walker, $menu_id ) 
    return 'Walker_Nav_Menu_Edit';
add_filter( 'wp_edit_nav_menu_walker', 'restore_edit_walker', 11, 2 );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download