WordPress how to modify <body> tag of a page by adding extra html attributes

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

how to create Popup window

If there is other way let me know.


thanks @ruvee

Wrote step by step instruction here: update body tag

here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Solution 1

There is a hook you could use called wp_footer. You could add extra attributes to your body tag using pure/vanilla javascript like this:

add_action("wp_footer", "your_theme_adding_extra_attributes");

function your_theme_adding_extra_attributes(){
    ?>
    <script>
        let body = document.getElementsByTagName("body");
        body[0].setAttribute("onBlur", "window.focus()"); 
    </script>
<?php }

Code goes into the functions.php file of your active theme.

This answer has been tested on wordpress 5.8 and works.

Wordpress how to modify <body> tag of a page by adding extra html attributes


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:

  • Only on archive pages:
    • is_archive() would be a proper conditional check
  • Only on the index of your blog:
    • is_home() would be a conditional check.
  • Only on front page that may or may not be your blog index:
    • is_front_page() would be a conditional check
  • etc…

UPDATE (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 its body tag, you could use the following snippet:

# The following code would only get executed on yourwebsite.com/blah

add_action("wp_footer", "your_theme_adding_extra_attributes");

function your_theme_adding_extra_attributes(){
    if(is_page("blah")){ ?>
    <script>
        let body = document.getElementsByTagName("body");
        body[0].setAttribute("onBlur", "window.focus()"); 
    </script>
<?php }
}

is_pageDocs


For people who would need to add an extra class NOT an extra attributes, there is a hook called body_class and there are too many answers already on Stackoverflow, such as:

Solution 2

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.

add_filter('body_class', 'bt_add_attr_to_body');
function bt_add_attr_to_body ($classes) {
    echo 'onBlur="window.focus()"';
    
    return $classes;
}

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

add_filter('body_class', 'bt_add_attr_to_body');
function bt_add_attr_to_body ($classes) {
    if (is_front_page()) echo 'onBlur="window.focus()"';
    
    return $classes;
}

Based on where you want this attribute to appeare, you will need to create the appropriate condition.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

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

Leave a Reply