Products
GG网络技术分享 2025-03-18 16:05 0
刚做的新网站,就发现有robot在采集,导致自己服务器运行卡顿甚至崩溃,而很多采集插件都是针对feed的,所以关闭feed,可以在一定程度防止采集。
除了通过插件实现关闭feed,还有以下几种方法:
//关闭rss feed功能function disable_all_feeds() {
wp_die(__('<h1>本博客不提供Feed,请访问网站<a href="'.get_bloginfo('url').'">首页</a>!</h1>'));
}
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. 如何才能彻彻底底地禁用WordPress的feed功能,连渣都不剩呢?我们可以使用下面的代码:
// 删除 wp_head 输入到模板中的feed地址链接3. 用插件比较简单,比如:Remove RSS Feedadd_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();
}
Demand feedback