check to see if hook is available


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 good way to achieve what I’m trying to do?

, Daveh0 2 years 2020-09-03T10:10:42-05:00 0 Answers 54 views 0

Leave an answer