Remove Comment Feeds in WordPress

Have you ever needed to remove the /feed/ from your WordPress posts, which is just a comments feed? Well, Alex Mateo did, and I don’t know when I became his programmer, but he writes to me as if I were…

Don’t have SEO friends, they never stop asking you for things…

Apparently he had a plugin that blocked this, but what the block does is return a 403, and Google kept trying to get in… so I wrote him a function to redirect to the post (actually I asked ChatGPT for it).

So I’ve already done it for him, and let’s share it in case someone else needs it. Maybe I’ll even get motivated and turn it into a WordPress plugin.

You need to place this code in your theme’s functions.php.

function remove_entry_feeds() {

    //Check whether we are in a post feed
    if (is_single() && is_feed()) {
        global $post;

        //Get the URL of the current post
        $post_url = get_permalink($post->ID);

        // Check that the post URL is valid
        if (!empty($post_url) && filter_var($post_url, FILTER_VALIDATE_URL)) {
            // Redirect the user to the post
            wp_redirect($post_url, 301); // 301 is the status code for 'Moved Permanently'
            exit;
        } 
    }
}
add_action('do_feed_rss2', 'remove_entry_feeds', 1, 1);
add_action('do_feed_atom', 'remove_entry_feeds', 1, 1);
remove_action('wp_head', 'feed_links_extra', 3);
add_filter('feed_links_show_comments_feed', '__return_false');

Finally, I recommend opening a post on your blog and checking that /comments/feed/ or the post URL/feed/ is not enabled.