老蒋有些时候看到有的网站没有使用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);

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();

}


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


扫描二维码,在手机上阅读!