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 |1 Answer
Reset to default 2No, 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
版权声明:本文标题:filters - check to see if hook is available 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742138899a2422493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:10Is 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