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?

Pseudo-code:

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x