Some say that RSS as a format of everyday content consumption is dead, and there’s no more space for RSS feeds in the era of social media. Even though RSS feeds have really lost their popularity in recent years, there are still many people who want to have control over their news feeds.
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.
How to hide the link to a custom post type RSS feed?
The first idea that might appear in your head is just to hide the link to a post type’s RSS feed that WordPress automatically inserts into your website’s <head> section. There’re two ways to achieve this.
The first one is by using removing the feed_links_extra() function from the wp_head action handlers. As the documentation says, this function displays “the links to the extra feeds such as category feeds“.
If you check this function’s source code, you’ll see that this function is responsible for displaying the RSS feed’s link on the custom post types’ archive pages. So, if you remove this action, the feed’s link of the current custom post type won’t be displayed.
But keep in mind that if you remove this action completely, you’ll remove the link for all post types. This means that we need to check if the current page is a post type’s archive page and the post type itself.
To remove this action, we’re going to use another action — wp. This is one of the earliest hooks we can use to remove actions and use conditional tags at the same time.
add_action( 'wp', 'remove_rss_feed_link_for_custom_post_type' );
function remove_rss_feed_link_for_custom_post_type() {
$post_type = 'job_posting';
if ( is_post_type_archive( $post_type ) ) {
remove_action('wp_head', 'feed_links_extra', 3 );
}
}
Now, if you check the source of your custom post type, you won’t see the RSS feed’s link.
Another way to implement the same result is to modify the result of the get_post_type_archive_feed_link() function.
The feed_links_extra() function we’ve already discussed uses this function to get a link to the current post type’s feed. You can modify its result by using the post_type_archive_feed_link filter hook. Why would you do this?
Let’s get back to the source code of feed_links_extra(). You can see there these lines of code:
if ( isset( $title ) && isset( $href ) ) {
echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( $title ) . '" href="' . esc_url( $href ) . '" />' . "\n";
}
As you might have guessed, if the $href variable is null, the <link> tag won’t be echoed. So, here’s another code snippet you can use to hide the RSS feed’s link of your post type:
add_filter( 'post_type_archive_feed_link', 'disable_rss_link_for_custom_post_type', 10, 2 );
function disable_rss_link_for_custom_post_type( $link, $feed ) {
$post_type = 'job_posting';
if ( get_post_type() === $post_type ) {
return null;
}
return $link;
}
And again, you won’t see the RSS feed’s link in the source code of your post type’s archive page.
How to disable the RSS feed of specific post type?
What’s the problem with the code snippets above? They hide links to the feeds, but they don’t disable the feeds. The feeds are still available by their links: you just need to know them.
So, instead of the RSS feeds, these links should return 404s.
This logic can be implemented by using the template_redirect hook.
This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
Both criteria match: we need to do a credit only if the current page is an RSS feed of our custom post type.
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();
}
}
Now, if you try to go to your post type’s RSS feed directly, you’ll see a regular 404 page.
Summary
I hope that this post will help you to manage your WordPress content the way you want. If you still have any questions or any issues with the code from this post, let me know about them in the comments.
By the way, recently, after a few years of free use, I bought a paid plan on Inoreader. This is a great RSS reader with a lot of features to filter and structure content from the RSS feeds you’re subscribed to. Its free plan allows you to add 150 feeds without paying a penny. So, I definitely recommend you to try Inoreader.
My WordPress blog has an RSS feed too, so you can subscribe to it if you don’t want to miss my new posts:
Otherwise, if you don’t like RSS and prefer more modern ways of data consuming, don’t forget to follow me on LinkedIn.
Thank you for your attention; see you in the next one!
How to remove feed in single post?
Example:
mydomain.com/my-single-post/feed/
https://vutruso.com/sua-loi-404-sau-khi-xoa-plugin-amp/feed/
I want remove or redirect if have /feed/ in last
Thank you.
Hi! Try this:
https://pastebin.com/TPNAAV62
Great, code working. thanks Bro!
How can access to mydomain.com/my-single-post/feed/ automatically redirect to the homepage?
I tried changing
get_template_part( 404 );
to
wp_redirect( home_url() );
but it not working.
Thanks
Was happy to help 🙂
Try this instead:
Thannks bro!