Skip to content

Instantly share code, notes, and snippets.

@elimn
Last active January 23, 2017 16:15
Show Gist options
  • Save elimn/9fc3a08878a0867bd10525d9e8bccbf1 to your computer and use it in GitHub Desktop.
Save elimn/9fc3a08878a0867bd10525d9e8bccbf1 to your computer and use it in GitHub Desktop.
MT | Remove filter or action added by an anonymous object
<?php
if ( ! function_exists( 'tribe_remove_anonymous_hook' ) ) {
/**
* Removes a filter or action added by anonymous objects
*
* Use this when you can not get the instance of a class attached to a hook.
* If the given method is attached multiple times to the hook, only one is removed.
*
* Example: tribe_remove_anonymous_hook( 'plugins_loaded', 'Tribe__Class', 'method_name' );
*
* @param string $tag Name of the filter or action.
* @param object|string $anonymous_class Object or classname that contains the method.
* @param string $method Method name.
* @param int $priority Priority the hook was attached to.
*/
function tribe_remove_anonymous_hook( $tag, $anonymous_class, $method, $priority = 10 ) {
global $wp_filter;
if ( ! isset( $wp_filter[ $tag ] ) ) {
return;
}
$wp_hook = $wp_filter[ $tag ];
// Ensure callbacks are attached to the priority
if ( ! isset( $wp_hook->callbacks[ $priority ] ) ) {
return;
}
foreach ( $wp_hook->callbacks[ $priority ] as $callback ) {
$function = $callback['function'];
// Skip callbacks that aren't methods
if ( ! is_array( $function ) ) {
continue;
}
// Check the class type and method name until we find a match.
if (
$function[0] instanceof $anonymous_class &&
$function[1] === $method
) {
remove_filter( $tag, $function, $priority );
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment