Last active
May 9, 2024 10:41
-
-
Save janw-me/ed66b8f02bb4075c57b835771d57f6ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove an object filter. | |
* | |
* @param string $tag Hook name. | |
* @param string|array $callback Class name or array with class and method. | |
* @param string|int|void $priority Priority, leave empty for all. | |
* @return bool true if removed, false if not. | |
* | |
* @link https://wordpress.stackexchange.com/a/140989 | |
*/ | |
function remove_object_filter( $tag, $callback, $priority = null ): bool { | |
/** | |
* The current hook @var \WP_Hook $wp_hook | |
*/ | |
$wp_hook = $GLOBALS['wp_filter'][ $tag ]; | |
if ( empty( $wp_hook ) ) { | |
return false; | |
} | |
if ( is_array( $callback ) ) { | |
$class_name = $callback[0]; | |
$method = $callback[1]; | |
} else { | |
$class_name = 'Closure'; | |
$method = null; | |
} | |
foreach ( $wp_hook as $p => $filter ) { | |
if ( ! is_null( $priority ) && ( (int) $priority !== (int) $p ) ) { | |
continue; | |
} | |
$remove = false; | |
foreach ( $filter as $identifier => $function ) { | |
$function = $function['function']; | |
if ( ! is_array( $function ) && $function instanceof \Closure && $class_name === 'Closure' ) { | |
$remove = true; | |
} elseif ( is_a( $function[0], $class_name ) || ltrim( $function[0], '\\' ) === ltrim( $class_name, '\\' ) ) { | |
$remove = ( $method && ( $method === $function[1] ) ); | |
} | |
if ( $remove ) { | |
unset( $wp_hook->callbacks[ $p ][ $identifier ] ); | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment