Disable WordPress RSS Feeds

How to disable all built-in RSS feeds in WordPress.

  • Two methods exist to disable all RSS feeds in WordPress; the author recommends Method #1 (removing feed rewrite rules) as the most efficient.
  • Method #1: Use the 'rewrite_rules_array' filter to unset any rewrite rule containing '(atom|rdf|rss|rss2|feed|commentsrss2)'. This cuts the number of rewrite rules PHP checks by about 50%.
  • Method #1 downside: you must flush permalinks afterward by visiting Settings > Permalinks in the WordPress admin.
  • Method #2: Hook into 'do_feed' and the 'do_feed_*' actions (rdf, rss, rss2, atom, and comment feeds) to intercept feeds before they are processed.
  • Avoid using wp_die() to halt feeds, since it returns a 500 Internal Server Error, which is bad for SEO. Instead, set the query to 404 and serve the theme's 404 template.
  • With either method, feed URLs return your theme's 404 / Page Not Found template.
  • Method #2 benefit: no permalink flush is required, but it is less efficient because WordPress still evaluates rewrite patterns before serving the 404.

There are other methods to do this, but I’ve found this approach to be the most efficient. Instead of

Removing feed rewrite rules is an effective way to disable all RSS feeds in a WordPress website. It’s also very efficient, it reduces the total number of rewrites that PHP checks for by about 50%. I use this myself when it’s necessary to remove RSS feeds from a WordPress website. It’s the best approach in my opinion.

/**
 * Disable All RSS Feeds
 */
add_action( 'rewrite_rules_array', function ( $rules ) {
  foreach ( $rules as $rule => $rewrite ) {
    if ( strstr( $rule, '(atom|rdf|rss|rss2|feed|commentsrss2)' ) ) {
      unset( $rules[$rule] );
    }
  }

  return $rules;
} );

After adding this function to your website’s theme or plugin, all feed related URL’s will return your theme’s 404 / Page Not Found template. The one downside to this approach is that you’ll need to flush your site’s permalinks. It’s easy to do, just visit the Settings > Permalinks page in your WordPress admin area. When this page is visited, WordPress will automatically flush/refresh all URL rewrites.

Method #2: Intercepting Feeds with do_feed Hooks

WordPress provides do_feed and do_feed_* hooks for filtering RSS and other XML feeds. These can be used to intercept feeds and disable them before they’re processed. Many of the examples around the web use the wp_die function to halt/deactivate feeds, but this isn’t ideal because it returns a 500 Internal Server Error. It’s bad for SEO and not ideal in other ways as well, I prefer serving up a 404 Page Not Found response instead which seems more appropriate.

/**
 * Intercept & Disable RSS/XML Feeds
 */
function kevinlearynet_disable_feeds() {
  global $wp_query;
  $wp_query->is_404 = true;
  status_header( 404 );
  nocache_headers();
  include get_query_template( '404' );
  exit;
}
add_action( 'do_feed', 'wp_disable_feeds', 1 );
add_action( 'do_feed_rdf', 'wp_disable_feeds', 1 );
add_action( 'do_feed_rss', 'wp_disable_feeds', 1 );
add_action( 'do_feed_rss2', 'wp_disable_feeds', 1 );
add_action( 'do_feed_atom', 'wp_disable_feeds', 1 );
add_action( 'do_feed_rss2_comments', 'wp_disable_feeds', 1 );
add_action( 'do_feed_atom_comments', 'wp_disable_feeds', 1 );

This has the same effect as the previous method, returning your theme’s 404 Page Not Found template when a feed URL is visited. I prefer the first method because it reduces rewrite rules and make the site more efficient. Why check the URL for RegExp rewrite patterns when we’re just going to serve a 404? If you’re more comfortable with this approach, though, I understand. The one benefit is that you don’t need to flush your site’s permalinks to get it working.