Created
November 3, 2021 19:45
-
-
Save petenelson/7fe8c1389091666b2540af58d31a1315 to your computer and use it in GitHub Desktop.
WordPress: Remove an anonymous object filter
This file contains hidden or 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 anonymous object filter. | |
* | |
* @param string $tag Hook name. | |
* @param string $class Class name | |
* @param string $method Method name | |
* @return void | |
*/ | |
function remove_anonymous_object_filter( $tag, $class, $method ) { | |
// From https://wordpress.stackexchange.com/questions/57079/how-to-remove-a-filter-that-is-an-anonymous-object. | |
if ( ! isset( $GLOBALS['wp_filter'][ $tag ] ) ) { | |
return; | |
} | |
$filters = $GLOBALS['wp_filter'][ $tag ]; | |
if ( empty ( $filters ) ) { | |
return; | |
} | |
foreach ( $filters as $priority => $filter ) { | |
foreach ( $filter as $identifier => $function ) { | |
if ( is_array( $function ) && is_a( $function['function'][0], $class ) && $method === $function['function'][1] ) { | |
remove_filter( $tag, [ $function['function'][0], $method ], $priority ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment