老蒋有些时候看到有的网站没有使用 WordPress 默认自带的 RSS Feed 订阅功能,而是将订阅工单单独的二级域名或者分离出去的。这样一来可以提高订阅的多样性和降低负载。在这篇文章中,我们要解决的问题是将默认的 RSS Feed 关闭,然后再想着后面如何修改订阅 URL 地址。

1、不开放 RSS 功能

function disable_all_feeds() {   wp_die( '本站不提供 feed' );}add_action('do_feed', 'disable_all_feeds', 1);add_action('do_feed_rdf', 'disable_all_feeds', 1);add_action('do_feed_rss', 'disable_all_feeds', 1);add_action('do_feed_rss2', 'disable_all_feeds', 1);add_action('do_feed_atom', 'disable_all_feeds', 1);
Plain text

2、彻底关闭
// 删除 wp_head 输入到模板中的 feed 地址链接add_action( 'wp_head', 'wpse33072_wp_head', 1 );function wpse33072_wp_head() {remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );}foreach( array( 'rdf', 'rss', 'rss2', 'atom' ) as $feed ) {add_action( 'do_feed_' . $feed, 'wpse33072_remove_feeds', 1 );}unset( $feed );// 当执行 do_feed action 时重定向到首页function wpse33072_remove_feeds() {wp_redirect( home_url(), 302 );
exit();}// 删除 feed 的重定向规则add_action( 'init', 'wpse33072_kill_feed_endpoint', 99 );function wpse33072_kill_feed_endpoint() {global $wp_rewrite;
$wp_rewrite->feeds = array();

// 运行一次后,记得删除下面的代码
flush_rewrite_rules();

}

Plain text

将代码添加到对应当前主题的 Functions.php 文件中。可以彻底先关闭 RSS 功能。