It’s possible to make a site totally inaccessible via feeds (RSS, Atom, RDF) with a function like this:
function itsme_disable_feed() {
wp_die( __( 'No feed available, please visit the <a href="'. esc_url( home_url( "https://wordpress.stackexchange.com/" ) ) .'">homepage</a>!' ) );
}
add_action('do_feed', 'itsme_disable_feed', 1);
add_action('do_feed_rdf', 'itsme_disable_feed', 1);
add_action('do_feed_rss', 'itsme_disable_feed', 1);
add_action('do_feed_rss2', 'itsme_disable_feed', 1);
add_action('do_feed_atom', 'itsme_disable_feed', 1);
add_action('do_feed_rss2_comments', 'itsme_disable_feed', 1);
add_action('do_feed_atom_comments', 'itsme_disable_feed', 1);
But that disables feeds for the entire site i.e. the main feed, feeds for categories, tags, comments, posts, pages, custom post types, etc.
How do I disable just the main feed and the main comments feed of the site? i.e. only make site.com/feed/
and site.com/comments/feed/
inaccessible.
Simply hiding feed using something like this (below) isn’t an option:
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
5 Answers
Very, very quick unscientific research suggests that there is only one query_var
set for the main feed. Other feeds such as category, tag, author feeds have more than one query_var
. Thus the following should kill the main feed but leave others intact.
add_filter(
'pre_get_posts',
function($qry) {
if ($qry->is_feed()) {
$fvars = array_filter($qry->query_vars);
if (1 == count($fvars)) {
wp_die( __( 'No feed available, please visit the <a href="'. esc_url( home_url( "https://wordpress.stackexchange.com/" ) ) .'">homepage</a>!' ) );
}
}
},
1
);
To remove the main comment feed, you need a small edit to check for the presence of $fvars['withcomments']
.
add_filter(
'pre_get_posts',
function($qry) {
if ($qry->is_feed() ) {
$fvars = array_filter($qry->query_vars);
if (1 == count($fvars) || isset($fvars['withcomments'])) {
wp_die( __( 'No feed available, please visit the <a href="'. esc_url( home_url( "https://wordpress.stackexchange.com/" ) ) .'">homepage</a>!' ) );
}
}
},
1
);
Be warned: Barely tested. Possibly buggy. Caveat emptor. No refunds.