A method that allows users to subscribe to a website and receive updates whenever new content is published, is RSS Feed, or Really Simple Syndication. While this is helpful for certain users, others can decide to limit the RSS Feed on their WordPress Web pages in a variety of ways. This article explains how to disable RSS feeds in WordPress using the code.
To do same you must have knowledge on how to edit the WordPress theme files and what impact will occur if any code break and unwanted mistake happens by you while placing these required code blocks.
WordPress has the in-built RSS feeds generation feature. You don’t need to install any plugins — WordPress handles everything for you. But sometimes, you need to disable the RSS feed for specific custom post types. In this post, I’m going to show you how to disable RSS feeds for specific custom post types.
Remove feed URL globally
To disable feed URL globally place below code in functions.php file which is located inside theme folder of your WordPress website setup
Hint – Navigate to the wp-content/themes/ directory and open the functions.php file in the active theme.
function disable_feed() { wp_die( __('No feed available, please visit the <a href="'. get_bloginfo('url') .'">homepage</a>!') ); } 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);
Once added, save the file, it will remove the feed links on your WordPress website. When users try to access the RSS feed, they will see a message saying “No feed available, please visit the homepage!”.
If you want to remove feeds links for a specific post types then use another action – wp
using a function like below mentioned method which is using removing the feed_links_extra()
function from the wp_head
action handlers.
add_action( 'wp', 'remove_feed_for_custom_post_type' ); function remove_feed_for_custom_post_type() { $post_type = 'article'; if ( is_post_type_archive( $post_type ) ) { remove_action( 'wp_head', 'feed_links', 2 ); remove_action('wp_head', 'feed_links_extra', 3 ); } }
Some of you may want to keep feed URLs on your WordPress website’s archive or any type of listing pages but want to hide on single post pages. In that case use below code
function remove_feeds_from_single_post () { if ( is_singular() ) { remove_action( 'wp_head', 'feed_links', 2 ); remove_action( 'wp_head', 'feed_links_extra', 3 ); } } add_action('wp', 'remove_feeds_from_single_post');
But above mentioned code blocks makes feed links are hidden on your WordPress website. To show 404 error page for those links you need add filter using template_redirect
hook.
add_action( 'template_redirect', 'show_404_for_disabled_cpt_feeds' ); function show_404_for_disabled_cpt_feeds() { $post_type = 'job_posting'; if ( is_feed() && get_post_type() === $post_type ) { global $wp_query; // Mark the current query as a 404 $wp_query->set_404(); // Return 404 HTTP status code instead of the default 200 status_header(404); // By default, this page returns XML, so we change the Content-Type header // Because we want to show a 404 page header('Content-Type: text/html; charset=utf-8'); // Render the 404 template get_template_part( 404 ); // You should exit from the script after that exit(); } }
Since the RSS Feed is outdated, it is a good idea to turn it off. There are a couple of different ways to do this. I recommend the plugin method if you are at all unfamiliar with coding and accessing the correct files you need.
This method is much faster and easier to perform.
I hope this tutorial was able to show you how easy it really is to turn off the RSS Feed in WordPress. If you have the right tools and steps in place, then the process is much less difficult.
If you still have any questions or any issues with the code from this post, let us know about them in the comments.