简单搜索了一下,发现网上都是下面这个方法,直接在主题的functions.php中加入下面的代码
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);
这种方法实现的效果是,当你打开博客的feed地址(如https://www.zhudc.com/feed)时,页面会直接显示设置的错误信息,feed代码已经被删除,也就是说这个feed地址仍然存在,而不是打开这个链接直接显示404。
代码版
如何才能彻彻底底地禁用WordPress的feed功能,连渣都不剩呢?我们可以使用下面的代码:
//删除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();
}
将以上php代码放入当前主题的functions.php中,然后登陆进入WordPress后台,随便打开一个页面,然后回来在主题的functions.php中将:flush_rewrite_rules(); 这行代码删除即可。