Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created November 3, 2021 19:45
Show Gist options
  • Save petenelson/7fe8c1389091666b2540af58d31a1315 to your computer and use it in GitHub Desktop.
Save petenelson/7fe8c1389091666b2540af58d31a1315 to your computer and use it in GitHub Desktop.
WordPress: Remove an anonymous object filter
<?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