首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

WordPress 文章搜尋引擎可見性控制:隱藏特定文章或僅允許爬蟲訪問

     編輯:PHP 綜合知識

SEO 策略中常需「部分內容不被索引」或「特定內容僅供搜尋引擎」。本文提供兩套完整解決方案:後台勾選隱藏與僅允許爬蟲訪問,含 Meta Box 介面、User-Agent 判斷、404/Noindex 兩種回應模式。

方案一:對搜尋引擎隱藏特定文章 (Noindex / 404)

核心邏輯

  1. 後台文章編輯頁新增「屏蔽搜尋引擎」核取方塊
  2. 前台請求時檢測 User-Agent 是否為已知爬蟲
  3. 符合條件時:返回 404 (徹底隱藏) 或輸出 noindex Meta 標籤 (保留頁面)

完整代碼 (放入 functions.php)

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 '';
//     }
// }

方案二:文章僅允許搜尋引擎訪問 (反向邏輯)

適用場景

  • SEO 導流頁面:內容對用戶無價值,但需被索引
  • API 文檔、Sitemap 專用頁
  • 測試頁面、遷移舊網址

完整代碼

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');

User-Agent 判斷優化建議

// 更完整的爬蟲特徵庫 (可維護於選項或常數)
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

重要限制與風險

  1. User-Agent 可偽造:技術型用戶可偽裝爬蟲 UA 繞過限制
  2. 頁面快取衝突:啟用快取外掛 (WP Rocket、W3 Total Cache) 時,動態判斷失效 → **需排除此類頁面快取**或用 JavaScript 端檢測
  3. 誤判風險:爬蟲特徵庫需持續維護,避免誤攔截正常流量
  4. Robots.txt 替代:大規模隱藏優先用 Disallow,此方案針對「精準單頁控制」

進階:整合至外掛/主題選項

// 註冊設定選項:預設隱藏類型
register_setting('ludou_se_options', 'ludou_se_default_mode', 'sanitize_text_field');
// 選項頁面表單:選擇 404 / Noindex / 403
// 前台邏輯根據選項動態切換回應模式

這兩套方案以「後台勾選 + 前台 UA 判斷」為核心,滿足從「隱藏採集內容」到「SEO 專用頁面」的多樣需求。部署時務必測試快取相容性,並定期更新爬蟲特徵庫。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved