In wordpress is there a way to set the body tag attribute on a Page like this :
<body onBlur="window.focus()">
WP Block editor does not allow editing the page html, because it is visual editor of course.
I would use this code so I can make a popup window stay always on top.
BTW I solved the problem how to popup the window, just dont know how to make it stay on top
If there is other way let me know.
thanks @ruvee
Wrote step by step instruction here: update body tag
2
Answers
There is a hook you could use called
wp_footer
. You could add extra attributes to yourbody
tag using pure/vanillajavascript
like this:Code goes into the
functions.php
file of your active theme.This answer has been tested on wordpress
5.8
and works.Running it on a specific page:
Depending on which page you would need to run it on, you could use different conditional statements. For example:
is_archive()
would be a proper conditional checkis_home()
would be a conditional check.is_front_page()
would be a conditional checkUPDATE (related to the second request that was not part of the first question)
If you have a page with a slug of
blah
and you want to modify itsbody
tag, you could use the following snippet:is_page
DocsFor people who would need to add an extra
class
NOT an extraattributes
, there is a hook calledbody_class
and there are too many answers already on Stackoverflow, such as:You could hook into the body class filter and do a echo for your attribute, not how filters are meant to work but it gets the job done without js.
I noticed from your comments that you would also like to limit to specific pages.
Lets say you want to to happen only on front page.
Youll need to add a if condition for front page, like this
Based on where you want this attribute to appeare, you will need to create the appropriate condition.