技术笔记
一名已转行的程序员WordPress 怎么关闭禁用网站的 RSS Feed 功能?
2023-05-19 / 代码笔记 / 220 次围观 / 0 次吐槽1、如果要彻底封禁网站全部 rss feed 功能,将下面代码加入 functions.php 里就行:
/** * 封禁所有 feed 功能 */ function dr_qa_disable_all_rss_feed() { wp_die( 'Feeds have been disabled.', 'WordPress Error', array('response'=>410) ); } add_action('do_feed', 'dr_qa_disable_all_rss_feed', 1); add_action('do_feed_rdf', 'dr_qa_disable_all_rss_feed', 1); add_action('do_feed_rss', 'dr_qa_disable_all_rss_feed', 1); add_action('do_feed_rss2', 'dr_qa_disable_all_rss_feed', 1); add_action('do_feed_atom', 'dr_qa_disable_all_rss_feed', 1);
2、如果是只封禁评论的 rss feed 功能的话,将下面代码加入 functions.php 里就行:
/** * * 禁止评论 feed */ add_filter( 'feed_links_show_comments_feed', '__return_false' ); function dr_qa_disable_comments_rss_feed($is_comment_feed) { if( $is_comment_feed ){ wp_die( 'Feeds for comments have been disabled.', 'WordPress Error', array('response'=>410) ); } } add_action('do_feed', 'dr_qa_disable_comments_rss_feed', 1); add_action('do_feed_rdf', 'dr_qa_disable_comments_rss_feed', 1); add_action('do_feed_rss', 'dr_qa_disable_comments_rss_feed', 1); add_action('do_feed_rss2', 'dr_qa_disable_comments_rss_feed', 1); add_action('do_feed_atom', 'dr_qa_disable_comments_rss_feed', 1);
3、如何才能彻彻底底地禁用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();这行代码删除即可。
推荐您阅读更多有关于“”的文章
Powered By Z-BlogPHP
Copyright 技术笔记 Rights Reserved.
额 本文暂时没人评论 来添加一个吧
发表评论