admin管理员组

文章数量:1323330

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?

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?

Share Improve this question edited Sep 3, 2020 at 16:34 Daveh0 asked Sep 3, 2020 at 14:31 Daveh0Daveh0 1912 silver badges13 bronze badges 4
  • The callback should only be run when the hook is run Plus you elaborate on what you tried with the global variable? – Jacob Peattie Commented 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 Commented 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 Commented 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 Commented Sep 3, 2020 at 16:26
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: filterscheck to see if hook is available