Home / Admin / Google Webmaster SEO Issue | Fix Duplicate without user-selected canonical
Duplicate Snippet

Embed Snippet on Your Site

Google Webmaster SEO Issue | Fix Duplicate without user-selected canonical

This snippet hooks into Rank Math’s rank_math/frontend/robots filter to modify how WordPress handles robots meta tags (index/follow directives) specifically for feeds (such as RSS, Atom, etc.). By detecting when a user or bot is viewing a feed, the code automatically sets the noindex and nofollow directives for that feed. This prevents search engines from indexing or following links within your site’s feed, which can help reduce duplicate content issues and keep your main content prioritized in search results.

What It Does
Checks if the current view is a feed
The is_feed() conditional tag determines whether the current request is for a feed.

Sets the robots meta directives for feeds
If it’s a feed, the code updates the $robots array to ['index' => 'noindex', 'follow' => 'nofollow'].

Returns the modified directives to Rank Math
The filter ensures that any feed pages on your site automatically carry the noindex, nofollow directives, instructing search engines not to index or follow links on those pages.

How to Implement Properly
Where to Place the Code

Child Theme’s functions.php: If you’re using a child theme, you can copy this snippet directly into the functions.php file.
Code Snippets Plugin: For a more organized approach (and to avoid losing customizations when updating your theme), you can install a plugin like “Code Snippets” and add the snippet there.
Ensure Rank Math is Active

This filter only works if you have the Rank Math SEO plugin installed and active. Otherwise, the hook rank_math/frontend/robots won’t be recognized.
Confirm the Snippet Works

Once the snippet is added, clear any caching (site cache, browser cache, etc.).
Visit a feed URL (e.g., yoursite.com/feed) and view the page’s source to confirm the presence of the noindex, nofollow meta directive in the robots meta tag.
By implementing this snippet, you help streamline your SEO efforts by preventing search engines from crawling and indexing your feed content, thus focusing search engine attention on the primary content of your site.

<10
Code Preview
php
<?php
add_filter( 'rank_math/frontend/robots', function( $robots ) {
    // Check if we are currently viewing a feed
    if ( is_feed() ) {
        $robots['index']  = 'noindex';
        $robots['follow'] = 'nofollow';
    }
    return $robots;
});

Comments

Add a Comment