Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Created November 25, 2014 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryhughes/b7e250223807ed97855e to your computer and use it in GitHub Desktop.
Save barryhughes/b7e250223807ed97855e to your computer and use it in GitHub Desktop.
Unhook actions/filters belonging to anonymous objects
<?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