I'm using a theme-specific, custom hook to add some custom attributes/values to the page's <body> element. There are however pages on my site that use a template that do not have that hook available and for certain reasons, I cannot modify that template or create a custom version in my child theme. I have a less elegant way of adding the attributes/values that I obviously only want to use when absolutely necessary.

Is there a way to check if a hook is available in the current template?


if(hook_is_available('custom_theme_hook')) {
  add_filter('custom_theme_hook', 'best_way');
} else {
  add_action('wp_footer', 'other_way');

I tried setting a Global variable that I could test against in my callback thinking it would only be executed IF the hook was available, thinking that the callback was only executed when there is a valid hook, but apparently this is not the case - the variable returned true on all pages regardless.

Is there a way to find out if a hook has run? Are there any other solutions that would allow me to add the attributes/values under all circumstances?

  • The callback should only be run when the hook is run Plus you elaborate on what you tried with the global variable? Sep 3, 2020 at 14:49
  • There is a solution to your problem, however, for some reason you didn't ask how to solve your problem, you asked how to implement a solution. I've posted an answer, but unfortunately you've asked how to implement a solution that won't work. Have you considered asking about your original problem instead? I cannot post the solution too your problem because that's not the question you asked.
    – Tom J Nowell
    Sep 3, 2020 at 15:06
  • I recommend asking this question: "How do I find out if a hook has ran?" This would allow you to conditionally do things in the wp_footer action. Also try to avoid making the examples super generic, you run the risk of getting an answer that is correct but can't be used because of something unique to your code
    – Tom J Nowell
    Sep 3, 2020 at 15:10
  • @TomJNowell - I feel I did ask that with Is there a way to check if a hook is available in the current template?, but I see how the question at the end of the post might have negated it. OP has been updated. I'd love to hear your solution.
    – Daveh0
    Sep 3, 2020 at 16:26

1 Answer 1


No, for this to work reliably you would need to register hooks, which can't be done. The only way to know if a template triggers a hook is to load the template and find out.

There is a solution to your problem, but testing if a hook is available is not that solution.

  • 1
    Gotcha. Care to share your solution?
    – Daveh0
    Sep 3, 2020 at 16:39
  • 1
    It would be very weird and confusing to post an unrelated answer that had nothing to do with the question. You should ask a new question for the new question.
    – Tom J Nowell
    Sep 3, 2020 at 18:08
  • new question posted - wordpress.stackexchange.com/questions/374273/…
    – Daveh0
    Sep 4, 2020 at 1:42

Your Answer

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

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