Last active
September 4, 2023 20:34
-
-
Save afragen/687abfdf1bd04ebc0ac88841ce673bdf to your computer and use it in GitHub Desktop.
Kludge for removing hook in WordPress when you don't have the exact callback object.
This file contains hidden or 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 | |
/** | |
* Kludge to remove hooks when I can't pass the precise object instance. | |
* | |
* @param string $hook_name The filter hook to which the function to be removed is hooked. | |
* @param callable|string|array $callback The callback to be removed from running when the filter is applied. | |
* This method can be called unconditionally to speculatively remove | |
* a callback that may or may not exist. | |
* @param int $priority The exact priority used when adding the original filter callback. | |
* | |
* @return void | |
*/ | |
private function remove_hook( $hook_name, $callback, $priority = 10 ) { | |
global $wp_filter; | |
if ( isset( $wp_filter[ $hook_name ] ) ) { | |
$hooks = $wp_filter[ $hook_name ]; | |
if ( isset( $wp_filter[ $hook_name ]->callbacks[ $priority ] ) ) { | |
$hooks = $wp_filter[ $hook_name ]->callbacks[ $priority ]; | |
foreach ( $hooks as $hook ) { | |
if ( is_array( $hook['function'] ) | |
&& ( $hook['function'][0] instanceof $callback[0] || $hook['function'][0] === $callback[0] ) | |
) { | |
$main_instance = $hook['function'][0]; | |
remove_filter( $hook_name, array( $main_instance, $callback[1] ), $priority ); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment