Fix the “Document Does Not Have a Main Landmark” Warning Using functions.php

by | Nov 1, 2025

If Google Lighthouse or PageSpeed Insights reports the warning “Document does not have a main landmark,” it means your site doesn’t include a <main> element or role="main" attribute that identifies the primary content area.

Developers often solve this by editing templates, but an easier way is through a functions.php snippet that handles it globally. You can do this safely in your child theme’s functions.php file without editing any core templates.

function add_main_landmark_to_content($content) {
    if (is_singular() && in_the_loop() && is_main_query()) {
        $content = '<main role="main">' . $content . '</main>';
    }
    return $content;
}
add_filter('the_content', 'add_main_landmark_to_content');

This snippet wraps your main post or page content in a <main> landmark. It works with most themes that use the standard WordPress loop and automatically resolves the accessibility warning after your next scan.

Why use functions.php?

This approach keeps the fix lightweight and update-safe. It’s perfect for anyone comfortable editing PHP, since you can implement it in just a few minutes without modifying theme templates or risking layout issues.

Why this matters

Adding a main landmark doesn’t just clean up an accessibility report — it makes your site easier to use for everyone, especially people relying on screen readers or keyboard navigation. In other words, you’re not just improving a score — you’re potentially helping someone out.

Google’s Lighthouse and PageSpeed tools include this check under the Accessibility category. While accessibility isn’t a direct ranking factor, resolving these issues can improve usability and engagement signals — both of which can indirectly strengthen your site’s SEO and search performance.

A small function, a cleaner audit, and a better experience for everyone — that’s a worthwhile line of code.


Photo by Makarand Mane

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *