• Resolved Javier Arias

    (@javierariascol)


    Hello everyone,

    I’m having trouble adding a custom attribute data-page="home" to the root <html> tag of the homepage on my WordPress site.

    First image. Here is the code to insert: data-page=”home”

    The purpose is to indicate that this is the home page, and only on this page will the preloader be loaded.

    Second image. Preloader targeting “Home” page

    I’ve tried using the following code in my theme’s functions.php file, but it doesn’t seem to be working as expected:

    <span style="font-size: 14.4px;">function add_custom_data_attribute_to_home($output) {
        if (is_home()) {
            $output = preg_replace('/<html(.*)(?=>)/', '<html$1 data-page="home">', $output, 1);
        }
        return $output;
    }
    add_filter('language_attributes', 'add_custom_data_attribute_to_home');</span>

    I’ve verified that I’m on the homepage when applying the code, but the data-page attribute isn’t being added to the <html> tag. Can anyone help me identify what might be going wrong or suggest an alternative solution? I greatly appreciate any advice you can provide.

    Thank you for your time and assistance!

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add a data-page=”home” custom attribute to the root tag of the document’ is closed to new replies.