Created
November 25, 2014 18:03
-
-
Save barryhughes/b7e250223807ed97855e to your computer and use it in GitHub Desktop.
Unhook actions/filters belonging to anonymous objects
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 | |
/** | |
* Sample approach for removing action/filter functions belonging | |
* to anonymous objects. | |
*/ | |
function remove_anonymous_objects_from_wp_hooks() { | |
global $wp_filter; | |
// Specific hooks and priorities we want to search | |
$hooks = [ | |
[ 'pre_get_posts', 100 ], | |
[ 'the_posts', 10 ] | |
]; | |
// Types we're interested in removing | |
$unwanted_types = [ | |
'someVendor\SomeClass', | |
'otherVendor\DifferentClass' | |
]; | |
// Look in the correct target areas | |
foreach ( $hooks as $hook_priority ) { | |
list( $hook, $priority ) = $hook_priority; | |
// Nothing set up on this hook/priority? Safe to skip | |
if ( ! isset( $wp_filter[$hook] ) || ! isset( $wp_filter[$hook][$priority] ) ) continue; | |
// Scan for callbacks within our unwanted types | |
foreach ( $wp_filter[$hook][$priority] as $action ) { | |
// Skip top-level functions and objects not on our unwanted types list | |
if ( ! is_array( $action['function'] ) ) continue; | |
if ( ! in_array( get_class( $action['function'][0] ), $unwanted_types ) ) continue; | |
// Purge | |
remove_action( $hook, array( $action['function'][0], $action['function'][1] ), $priority ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment