The provided code snippet disables all RSS feeds in WordPress.
The wpcode_snippet_disable_feed
function displays a custom message when someone tries to access an RSS feed. It uses the wp_die
function to terminate script execution and display the HTML message. The message encourages the user to visit the website’s homepage, providing a link to it.
The following add_action
lines hook the wpcode_snippet_disable_feed
function to the various feed actions. The 1
that is passed as the third parameter to each add_action
call gives the hooked function a high priority, ensuring it runs before any other functions hooked to the same action.
The remove_action
lines at the end of the snippet remove the RSS feed links from the website’s <head>
, so they’re not discoverable by web browsers and RSS feed readers.
/**
* Display a custom message instead of the RSS Feeds.
*
* @return void
*/
function wpcode_snippet_disable_feed() {
wp_die(
sprintf(
// Translators: Placeholders for the homepage link.
esc_html__( 'No feed available, please visit our %1$shomepage%2$s!' ),
' <a href="' . esc_url( home_url( '/' ) ) . '">',
'</a>'
)
);
}
// Replace all feeds with the message above.
add_action( 'do_feed_rdf', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_rss', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_rss2', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_atom', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_rss2_comments', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_atom_comments', 'wpcode_snippet_disable_feed', 1 );
// Remove links to feed from the header.
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );