如何彻底关闭 WordPress 的 RSS feed 功能?

要彻底关闭 WordPress 的 RSS feed 功能,可以使用以下两种方法之一:

方法 1:使用代码(推荐)

将以下代码添加到当前主题(或子主题)的 functions.php 文件中,以禁用所有 RSS/Atom 源并移除头部 feed 链接:

// 禁用所有 RSS/Atom 源
function disable_all_feeds() {
    wp_die( '本站不提供 feed,请访问 <a href="' . esc_url( home_url( '/' ) ) . '">首页</a>!' );
}
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 链接
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );

方法 2:彻底移除 Feed 规则

使用更彻底的代码(需运行一次后删除 flush_rewrite_rules()):

// 移除头部 feed 链接
add_action( 'wp_head', 'custom_remove_feed_links', 1 );
function custom_remove_feed_links() {
    remove_action( 'wp_head', 'feed_links', 2 );
    remove_action( 'wp_head', 'feed_links_extra', 3 );
}

// 重定向所有 feed 请求到首页
foreach ( array( 'rdf', 'rss', 'rss2', 'atom' ) as $feed ) {
    add_action( 'do_feed_' . $feed, 'custom_redirect_feeds', 1 );
}
unset( $feed );
function custom_redirect_feeds() {
    wp_redirect( home_url(), 302 );
    exit();
}

// 删除 feed 规则
add_action( 'init', 'custom_kill_feed_endpoint', 99 );
function custom_kill_feed_endpoint() {
    global $wp_rewrite;
    $wp_rewrite->feeds = array();
    flush_rewrite_rules(); // 运行后删除此行
}

将上述代码放入主题的 functions.php 文件中即可 。

方法 3:使用插件

  • Disable Feeds 插件:完全禁用所有 RSS/Atom 源,并将请求重定向到相应 HTML 内容或显示 404 错误 。
  • Remove RSS Feed 插件:通过上传插件目录并启用插件实现禁用 RSS 功能 。

完成设置后,记得删除 flush_rewrite_rules(); 这一行代码 。

本文摘自网络,不代表短经典网立场 https://www.duanjingdian.com/1324.html

上一篇 2025 年 7 月 11 日 下午12:59
下一篇 2024 年 10 月 12 日 下午1:56

相关推荐