怎么删除feed php,怎样关闭或删除WordPress程序默认的RSS feed功能
摘要:WordPress feed功能可以進行RSS閱讀器訂閱,讓讀者實時接收博客的更新內容。但rss在國內及當前的訪客中并不...
WordPress feed功能可以進行RSS閱讀器訂閱,讓讀者實時接收博客的更新內容。但rss在國內及當前的訪客中并不普及,而且會有機器人來采集復制你的文章feed,造成不必要的資源消耗。
關閉wordpress feed的方式很多,如在WordPress后臺 – 設置 – 閱讀中設置,只輸出文章摘要不輸出全文。
不想開放wordpress博客的feed功能,該怎么設置呢?
.
我們可以直接在主題的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);
1
2
3
4
5
6
7
8
functiondisable_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地址時feed地址仍然存在,而不是打開這個鏈接直接顯示404。會直接顯示設置的錯誤信息。
如何徹底移除并關閉WordPress的RSS feed代碼版
如何才能徹底地禁用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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 刪除 wp_head 輸入到模板中的feed地址鏈接
add_action('wp_head','wpse33072_wp_head',1);
functionwpse33072_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 時重定向到首頁
functionwpse33072_remove_feeds(){
wp_redirect(home_url(),302);
exit();
}
// 刪除feed的重定向規則
add_action('init','wpse33072_kill_feed_endpoint',99);
functionwpse33072_kill_feed_endpoint(){
global$wp_rewrite;
$wp_rewrite->feeds=array();
// 運行一次后,記得刪除下面的代碼
flush_rewrite_rules();
}
將以上函數代碼放入當前主題的functions.php中即可。
總結
以上是生活随笔為你收集整理的怎么删除feed php,怎样关闭或删除WordPress程序默认的RSS feed功能的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 想要转行产品经理,可是社招都要有产品经理
- 下一篇: 搭建并配置keil嵌入式开发环境教程及s
