SEO 策略中常需「部分內容不被索引」或「特定內容僅供搜尋引擎」。本文提供兩套完整解決方案:後台勾選隱藏與僅允許爬蟲訪問,含 Meta Box 介面、User-Agent 判斷、404/Noindex 兩種回應模式。
noindex Meta 標籤 (保留頁面)ID, '_ludou_hide_from_se', true);
echo '';
}
// 2. 儲存設定
function ludou_se_save($post_id) {
if (!isset($_POST['ludou_se_nonce']) || !wp_verify_nonce($_POST['ludou_se_nonce'], 'ludou_se_action')) return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
update_post_meta($post_id, '_ludou_hide_from_se', isset($_POST['ludou_hide_se']) ? '1' : '0');
}
add_action('save_post', 'ludou_se_save');
// 3. 前台攔截 (兩種模式擇一)
// 模式 A:返回 404 (徹底隱藏,無內容輸出)
function ludou_se_block_404() {
if (!is_singular()) return;
$hide = get_post_meta(get_the_ID(), '_ludou_hide_from_se', true);
if (!$hide) return;
$bots = ['googlebot', 'bingbot', 'slurp', 'duckduckbot', 'baiduspider', 'yandex', 'sogou', 'ia_archiver'];
$ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
foreach ($bots as $bot) {
if (strpos($ua, $bot) !== false) {
status_header(404);
nocache_headers();
die; // 直接終止,不輸出任何內容
}
}
}
add_action('template_redirect', 'ludou_se_block_404');
// 模式 B:輸出 noindex (保留頁面可訪問,但不索引)
// 將上方 add_action 改為:
// add_action('wp_head', 'ludou_se_noindex');
// function ludou_se_noindex() {
// if (!is_singular()) return;
// if (get_post_meta(get_the_ID(), '_ludou_hide_from_se', true)) {
// echo '';
// }
// }
ID, '_ludou_se_only', true);
echo '';
}
// 2. 儲存
function ludou_se_only_save($post_id) {
if (!isset($_POST['ludou_se_only_nonce']) || !wp_verify_nonce($_POST['ludou_se_only_nonce'], 'ludou_se_only_action')) return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
if (isset($_POST['ludou_se_only'])) {
update_post_meta($post_id, '_ludou_se_only', '1');
} else {
delete_post_meta($post_id, '_ludou_se_only');
}
}
add_action('save_post', 'ludou_se_only_save');
// 3. 攔截:非爬蟲且非登入用戶 → 拒絕訪問
function ludou_se_only_enforce() {
if (!is_singular()) return;
$enabled = get_post_meta(get_the_ID(), '_ludou_se_only', true);
if (!$enabled) return;
// 已登入用戶 (管理員/編輯) 不受限
if (is_user_logged_in()) return;
$bots = ['googlebot', 'bingbot', 'slurp', 'duckduckbot', 'baiduspider', 'yandex', 'sogou'];
$ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
$is_bot = false;
foreach ($bots as $bot) {
if (strpos($ua, $bot) !== false) { $is_bot = true; break; }
}
if (!$is_bot) {
wp_die('此內容僅供搜尋引擎索引,請透過搜尋結果訪問。', '存取受限', ['response' => 403]);
}
}
add_action('template_redirect', 'ludou_se_only_enforce');
// 更完整的爬蟲特徵庫 (可維護於選項或常數)
const KNOWN_BOTS = [
'googlebot', 'bingbot', 'slurp', 'duckduckbot', 'baiduspider', 'yandexbot', 'sogou',
'exabot', 'facebot', 'twitterbot', 'linkedinbot', 'telegrambot', 'whatsapp',
'applebot', 'ia_archiver', 'pinterest', 'redditbot', 'discordbot',
// 通用特徵
'bot', 'crawler', 'spider', 'slurp', 'archiver'
];
function is_search_bot(): bool {
$ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
foreach (KNOWN_BOTS as $bot) {
if (strpos($ua, $bot) !== false) return true;
}
return false;
}
| 模式 | 回應 | 搜尋引擎行為 | 用戶體驗 | 適用場景 |
|---|---|---|---|---|
| 404 | HTTP 404 + 空白頁 | 移除索引、不跟蹤連結 | 看到 404 頁面 | 私密內容、測試頁、已下架商品 |
| Noindex | HTTP 200 + <meta name="robots" content="noindex"> |
不索引、但可跟蹤連結 | 正常瀏覽 | 重複內容、分頁頁面、感謝頁、內部搜尋結果 |
| 403 (僅爬蟲) | HTTP 403 + 錯誤訊息 | 正常索引 | 看到拒絕訊息 | SEO 專用頁、API 文檔、Sitemap |
Disallow,此方案針對「精準單頁控制」// 註冊設定選項:預設隱藏類型
register_setting('ludou_se_options', 'ludou_se_default_mode', 'sanitize_text_field');
// 選項頁面表單:選擇 404 / Noindex / 403
// 前台邏輯根據選項動態切換回應模式
這兩套方案以「後台勾選 + 前台 UA 判斷」為核心,滿足從「隱藏採集內容」到「SEO 專用頁面」的多樣需求。部署時務必測試快取相容性,並定期更新爬蟲特徵庫。