Assume that some other plugin or theme uses a add_action hook. Is there a way to determine if a specific hook is being used by someone else?
Example: assume that a plugin has this code
add_action ( 'publish_post', 'myCustomFunction' );
function myCustomFunction() {
// does something
}
Can my plugin ‘look’ for the use of that hook into ‘publish_post’? And can I remove that particular action hook before it fires?
You can use has_action()
. It returns the priority of the hook if it exists, otherwise false
. So check that the priority is not equal to false
(and not just false-y, or being hooked at 0
will appear false) then use that to remove the hook.
$priority = has_action( 'publish_post', 'myCustomFunction' );
if ( $priority !== false ) {
remove_action( 'publish_post', 'myCustomFunction', $priority );
}
There’s also has_filter()
, which does exactly the same thing (has_action()
just calls has_filter()
).