WordPress wp_head() 預設輸出大量 Meta 標籤、連結、版本資訊,不僅增加頁面體積,更洩露系統細節增加攻擊面。本文系統整理頭部清理代碼,逐項解析各 remove_action 目的、安全影響與保留建議,提供生產環境可直接部署的完整方案。
<?php
// === Feed 相關 ===
remove_action('wp_head', 'feed_links', 2); // 文章/評論 Feed
remove_action('wp_head', 'feed_links_extra', 3); // 分類/標籤 Feed
// === 離線編輯器介面 ===
remove_action('wp_head', 'rsd_link'); // RSD (Really Simple Discovery)
remove_action('wp_head', 'wlwmanifest_link'); // Windows Live Writer
// === 關係鏈接 ===
remove_action('wp_head', 'index_rel_link'); // 首頁連結
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // 父文章
remove_action('wp_head', 'start_post_rel_link', 10, 0); // 第一篇文章
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); // 上/下篇
// === 版本資訊 ===
remove_action('wp_head', 'wp_generator'); // WordPress 版本號
// === 短連結 ===
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('template_redirect', 'wp_shortlink_header', 11, 0);
// === 近期評論 Widget 樣式 ===
add_action('widgets_init', 'my_remove_recent_comments_style');
function my_remove_recent_comments_style() {
global $wp_widget_factory;
remove_action('wp_head', array(
$wp_widget_factory->widgets['WP_Widget_Recent_Comments'],
'recent_comments_style'
));
}
?>
| Action | 輸出內容 | 安全風險 | 建議 | 保留場景 |
|---|---|---|---|---|
| feed_links | <link rel="alternate" type="application/rss+xml"> | 低 | ✅ 移除 | 需瀏覽器自動發現 Feed |
| feed_links_extra | 分類/標籤/作者 Feed | 低 | ✅ 移除 | 複雜分類站點 |
| rsd_link | <link rel="EditURI" type="application/rsd+xml"> | 中(暴露 xmlrpc.php) | ✅ 移除 | 使用 MarsEdit 等離線編輯器 |
| wlwmanifest_link | <link rel="wlwmanifest"> | 中(針對性攻擊) | ✅ 移除 | 使用 Windows Live Writer |
| index_rel_link | <link rel="index"> | 低 | ✅ 移除 | 極少數 SEO 工具需求 |
| parent_post_rel_link | <link rel="up"> | 低 | ✅ 移除 | 層級文章結構站點 |
| start_post_rel_link | <link rel="start"> | 低 | ✅ 移除 | 系列文章導航 |
| adjacent_posts_rel_link | <link rel="prev/next"> | 低 | ⚠️ 可保留 | 瀏覽器預載入、SEO 導航 |
| wp_generator | <meta name="generator" content="WordPress x.x.x"> | 高(版本指纹) | ✅ 必須移除 | 無 |
| wp_shortlink_wp_head | <link rel="shortlink"> | 低 | ✅ 移除 | 使用 Jetpack 短連結 |
| recent_comments_style | 內聯 CSS 樣式 | 低 | ✅ 移除 | 使用原生近期評論 Widget |
<meta name="generator" content="WordPress 6.4.2" />
攻擊者可針對特定版本已知漏洞發起自動化攻擊。無任何功能損失,生產環境強制移除。
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://example.com/xmlrpc.php?rsd" /> <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://example.com/wp-includes/wlwmanifest.xml" />
xmlrpc.php 端點,易遭暴力破解、DDoS(Pingback 攻擊).htaccess/Nginx 封鎖 xmlrpc.php// 如確不需要,可移除
remove_action('wp_head', 'rel_canonical');
作用:指定規範 URL,防重複內容扣分(分頁、參數、多 URL 同內容)
建議:保留,除非你有完整自定 Canonical 策略
remove_action( string $tag, callable $function_to_remove, int $priority = 10, int $accepted_args = 1 ) : bool
wp_head)常見失敗原因:優先序不匹配(如預設 10 vs 實際 20)、方法在類中需用 array($this, 'method') 或 array('Class', 'method')
function conditional_head_cleanup() {
// 單篇文章保留相鄰導航
if (is_single()) return;
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
}
add_action('wp_head', 'conditional_head_cleanup', 1); // 優先執行
<head> 區域generator、rsd、wlwmanifest、rel="prev" 等關鍵字curl -I https://example.com 檢查 Link 標頭(shortlink_header)[✅] wp_generator - 版本洩露,必移除 [✅] rsd_link - XML-RPC 暴露,必移除 [✅] wlwmanifest_link - WLW 介面,必移除 [✅] feed_links* - 無 Feed 需求,移除 [✅] *_rel_link - 關係鏈接,移除 [✅] shortlink* - 短連結,移除 [✅] recent_comments_style - Widget 樣式,移除 [⚠️] rel_canonical - SEO 關鍵,保留 [⚠️] adjacent_posts_rel - 瀏覽器預載/SEO,可保留
WordPress 頭部清理是「減法優化」的典型實踐:移除無用代碼減少頻寬、降低攻擊面、提升爬蟲效率。掌握 remove_action 機制與各 Action 用途,並建立專案標準清單,是 WordPress 專案上線前的必做項目。記住:**預設不安全,精簡才安全**。