How to Disable Feed URLs in WordPress with Simple Code

RSS feeds are automatically enabled on WordPress websites to provide content syndication for blogs. However, in some cases, you might want to disable feed URLs, especially if your site doesn’t need them or if you’re trying to prevent content scraping. This tutorial will walk you through the process of disabling feed URLs in WordPress using simple code.

How to disable FEED URLs in WordPress website

Why Disable Feed URLs in WordPress?

While RSS feeds are helpful for blog readers, there are situations where disabling them might be beneficial:

  1. Prevent Duplicate Content: Search engines might index your feed URLs, which can result in duplicate content issues.
  2. Limit Content Scraping: RSS feeds can be exploited by scrapers to steal your content.
  3. Streamlined Website: If your site is not blog-focused, you may not need RSS feeds at all.

With a simple snippet of code, you can disable feeds and redirect users to the original content page.


Code to Disable Feed URLs

Here’s how to remove feed URLs from the WordPress header and redirect feed requests:

// Remove feed URLs from the header
function disable_feed_links() {
    remove_action('wp_head', 'feed_links', 2);
    remove_action('wp_head', 'feed_links_extra', 3);
}
add_action('init', 'disable_feed_links');

// Redirect feed requests to the original page
function redirect_feed_requests_to_original_page($query) {
    if ($query->is_feed) {
        global $wp;
        $current_url = home_url(add_query_arg(array(), $wp->request));
        $original_url = preg_replace('/\/feed(\/.*|$)/', '', $current_url);
        wp_redirect($original_url, 301);
        exit;
    }
}
add_action('parse_query', 'redirect_feed_requests_to_original_page');

How This Code Works

  1. Removing Feed Links from the Header:
    The disable_feed_links() function uses remove_action to stop WordPress from outputting feed links in the <head> section of your website.
    • feed_links: Removes the primary RSS feed links.
    • feed_links_extra: Removes additional feed links (e.g., for categories or comments).
  2. Redirecting Feed Requests:
    The redirect_feed_requests_to_original_page() function checks if the current query is a feed request using $query->is_feed. If it is, the code:
    • Retrieves the current URL.
    • Strips out /feed from the URL using preg_replace.
    • Redirects users to the original page using wp_redirect with a 301 (permanent) status code.

How to Use the Code

  1. Add Code to Your Theme:
    Paste the above snippet into your theme’s functions.php file or use a custom plugin to add it.
  2. Test the Feed URLs:
    After implementing the code:
    • Try visiting any feed URL, such as yoursite.com/feed. You should be redirected to the original page.
    • Confirm that feed links no longer appear in the source code of your site’s header.

Important Notes

  • Backup Your Site: Always back up your WordPress site before making changes to theme or plugin files.
  • Compatibility with Plugins: Ensure the changes do not interfere with plugins that rely on RSS feeds, such as email subscription tools or aggregators.
Trustpilot