Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save azizultex/dd55c5c0518a427ae3a5552f683eb29b to your computer and use it in GitHub Desktop.
Save azizultex/dd55c5c0518a427ae3a5552f683eb29b to your computer and use it in GitHub Desktop.
Remove a hook, filters from a Plugin Class Method WordPress
/**
* Allow to remove method for an hook when, it's a class method used and class don't have variable, but you know the class name :)
* source: https://github.com/herewithme/wp-filters-extras
* More: http://wordpress.stackexchange.com/questions/57079/how-to-remove-a-filter-that-is-an-anonymous-object
*/
function remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $method_name = '', $priority = 0 ) {
global $wp_filter;
// Take only filters on right hook name and priority
if ( !isset($wp_filter[$hook_name][$priority]) || !is_array($wp_filter[$hook_name][$priority]) )
return false;
// Loop on filters registered
foreach( (array) $wp_filter[$hook_name][$priority] as $unique_id => $filter_array ) {
// Test if filter is an array ! (always for class/method)
if ( isset($filter_array['function']) && is_array($filter_array['function']) ) {
// Test if object is a class, class and method is equal to param !
if ( is_object($filter_array['function'][0]) && get_class($filter_array['function'][0]) && get_class($filter_array['function'][0]) == $class_name && $filter_array['function'][1] == $method_name ) {
unset($wp_filter[$hook_name][$priority][$unique_id]);
}
}
}
return false;
}
/* remove vendors images */
remove_filters_for_anonymous_class('woocommerce_archive_description', 'WC_Product_Vendors_Vendor_Frontend', 'display_vendor_logo_profile', 10);
@azizultex
Copy link
Author

In order to override a plugin shortcode in WordPress, follow: https://gist.github.com/azizultex/ae7ddab5c4a388c1056e0bfa0a9473ad

@sanderart
Copy link

I've found that in my project code works only in use
$wp_filter[$hook_name]->callbacks[$priority]
instead
$wp_filter[$hook_name][$priority]
because each hook is an Object with list of callback functions in callbacks property.
Maybe the newest WP version has such architecture, but I had discover it when print_r($wp_filter).

@sabrina-zeidan
Copy link

That's right, it's WP_Hook class in the recent WP versions, so $wp_filter[$hook_name]->callbacks[$priority] only will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment