Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.