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?

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

| improve this answer | |
  • Sudden thought: is there then a way to tell where that action was defined (the source file that contains that do_action)? – Rick Hellewell May 24 '18 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. – Jacob Peattie May 24 '18 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'? – Rick Hellewell May 25 '18 at 1:41
  • Do you mean add_action()? – Jacob Peattie May 25 '18 at 1:43
  • Whoops. Yes. Would be interesting to know what file has the add_action function, without having to scan all files. – Rick Hellewell May 25 '18 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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