Skip to content

Instantly share code, notes, and snippets.

@mrmu
Created December 6, 2021 03:46
Show Gist options
  • Save mrmu/13d95009b681ff6168258cadd0ea2e72 to your computer and use it in GitHub Desktop.
Save mrmu/13d95009b681ff6168258cadd0ea2e72 to your computer and use it in GitHub Desktop.
[WordPress] Remove the hook functions defined in object of class (移除寫在物件裡的 hook functions)
<?php
// Remove hook functions (include actions and filters) that were defined in instances of class.
function remove_hooked_functions_in_obj($hook_tag = false, $class_name = '', $function_name, $priority = 10) {
// Visit all hook tags if existed
if (!empty($GLOBALS['wp_filter']) && !empty($GLOBALS['wp_filter'][$hook_tag])) {
foreach($GLOBALS['wp_filter'][$hook_tag]->callbacks[$priority] as $callback) {
// You can use get_class($obj) to get the class name which object belong
if (
$callback['function'] &&
(is_a( $callback['function'][0], $class_name )) &&
($callback['function'][1] == $function_name)
){
$callable = [ $callback['function'][0], $function_name ];
$GLOBALS['wp_filter'][$hook_tag]->remove_filter( $hook_tag, $callable, $priority );
return true;
break;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment