3

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?

1 Answer 1

2

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()).

5
  • Sudden thought: is there then a way to tell where that action was defined (the source file that contains that do_action)? May 24, 2018 at 3:16
  • I don't believe so. When adding hooks it's possible that the file with the do_action() hasn't even been loaded yet. May 24, 2018 at 5:07
  • Looking for the file that contained the do_action, not the file that will process it. So if my_plugin.php contains a 'do_action', is there any way to log/determine that my_plugin.php was the 'culprit'? May 25, 2018 at 1:41
  • Do you mean add_action()? May 25, 2018 at 1:43
  • Whoops. Yes. Would be interesting to know what file has the add_action function, without having to scan all files. May 25, 2018 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.