Disable only the individual-post comment feeds
An alternate version of mor7ifer's answer, this one only disables the per-post comment feeds, and not the site-wide comments feed (by checking for is_single()
before doing wp_die()
).
It also uses the 410
error code which means GONE
and seems appropriate for this use-case (at least for me, where these URLs already existed and are indexed, and I want to unequivocally tell Google and friends to stop indexing).
function wpse45941_disable_feed( $comments ) {
if( $comments and is_single() ) {
wp_die( 'Feeds for comments on single posts have been disabled.', 410 );
}
}
add_action('do_feed', 'wpse45941_disable_feed',1 );
add_action('do_feed_rdf', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss2', 'wpse45941_disable_feed',1 );
add_action('do_feed_atom', 'wpse45941_disable_feed',1 );
Disable all comment feeds
Here's the same code with the 410
HTTP status, but it will also disable the sitewide comments feed (and shows an appropriate message):
function wpse45941_disable_feed( $comments ) {
if( $comments ) {
wp_die( 'Feeds for comments have been disabled.', 410 );
}
}
add_action('do_feed', 'wpse45941_disable_feed',1 );
add_action('do_feed_rdf', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss2', 'wpse45941_disable_feed',1 );
add_action('do_feed_atom', 'wpse45941_disable_feed',1 );
Note: This doesn't fix the <item>
for each post in the normal RSS feed
On further inspection, I've found the following error when validating the main (posts) RSS feed for my site:
It seems WP is still inserting the comments RSS tag into the feed, but because that feed is disabled, it generates an error.
RSS still works, but I'm looking for a solution to get rid of it. This Core Trac issue is relevant