admin管理员组文章数量:1323335
This is similar to How to add data- attribute to <body> tag but different in that I am n ow searching for a solution that does not require modifications to the template file.
I have a requirement to add an attribute to the <body>
tags in set of pages for which I cannot modify the template php file. For most of them, there is a theme-specific hook I'm using which is working fine. There are others that do not contain that hook. for these, I am able to achieve what I'm going for by essentially rewriting the final markup... with the attribute inserted - code used came from kfriend's answer at .
So I have these 2 approaches:
add_filter('theme_specific_hook', 'main_way');
and
add_filter('wp_footer', 'alternate_way');
The end result of each is
<body data-my-attr="MY VALUE" class="all-of the required-classes" ...>
which satisfies my requirement. The problem here is that I don't have a way to tell WP when NOT to run the 2nd, less ideal filter.
I don't know if this is an unnecessary and/or overly complicated workaround, but more importantly, I don't even know if trying to conditionally take 1 of these 2 approaches is the most efficient way to go.
Does anyone have any input on this approach as far as best practices and or feasibility are concerned? Any other suggestions for ways to insert this attribute/value into the <body>
tag of these pages regardless of their template?
This is similar to How to add data- attribute to <body> tag but different in that I am n ow searching for a solution that does not require modifications to the template file.
I have a requirement to add an attribute to the <body>
tags in set of pages for which I cannot modify the template php file. For most of them, there is a theme-specific hook I'm using which is working fine. There are others that do not contain that hook. for these, I am able to achieve what I'm going for by essentially rewriting the final markup... with the attribute inserted - code used came from kfriend's answer at https://stackoverflow/questions/772510/wordpress-filter-to-modify-final-html-output.
So I have these 2 approaches:
add_filter('theme_specific_hook', 'main_way');
and
add_filter('wp_footer', 'alternate_way');
The end result of each is
<body data-my-attr="MY VALUE" class="all-of the required-classes" ...>
which satisfies my requirement. The problem here is that I don't have a way to tell WP when NOT to run the 2nd, less ideal filter.
I don't know if this is an unnecessary and/or overly complicated workaround, but more importantly, I don't even know if trying to conditionally take 1 of these 2 approaches is the most efficient way to go.
Does anyone have any input on this approach as far as best practices and or feasibility are concerned? Any other suggestions for ways to insert this attribute/value into the <body>
tag of these pages regardless of their template?
1 Answer
Reset to default 0Use both hooks, and call did_action
to find out if the template hook fired or not.
https://developer.wordpress/reference/functions/did_action/
did_action
will return the number of times a hook/action has fired. With this you can return early in the footer action
本文标签: filtersHow to add attributes to ltbodygt tag when template cannot be directly modified
版权声明:本文标题:filters - How to add attributes to <body> tag when template cannot be directly modified 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742137579a2422437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
final_output
hook back towp_footer
so it isn't confusing – Tom J Nowell ♦ Commented Sep 4, 2020 at 9:27