How to remove_action which activates an object method

24 Oct

add_action/add_filter can accept either a function name or an object and a method name as the ‘function’ parameter. The problem is that remove_action/remove_filter can remove only actions which use a function name or a static class methos as their ‘function’, object methods type of ‘function’ can not be removed at least in the 2.8 version.

There is no official way to remove, but if needed you can apply the following hack which tries to find the ‘function’ in the internal structures, by looping over all the functions while trying to find the best match and remove it.


// assuming this was the add_action being used

$f =& new Custom_Image_Header();
add_action('admin_menu', array(&$f, 'init'));

// this code will remove it

foreach ($GLOBALS['wp_filter']['admin_menu'][10] as $f => $v) {
  if (substr($f, 0, 23) == 'Custom_Image_Headerinit') {
    remove_action('admin_menu', $f);
  }
}

This code has two important drawbacks

  1. It may remove the wrong object if more then one object of the same class ware added as a function
  2. Similar class names might confuse the code. In this case it might remove call to the ‘nit’ method of the ‘Custom_Image_Headeri’ class

No comments yet

Leave a Reply