大家都知道的,WordPress 站點中訪客留言後,如果訪客留下了站點連結的話,WordPress 預設是會以描文字的形式給訪客新增一個外鏈的。雖然 WordPress 預設的都會給連結加上 nofollow 屬性來防止垃圾連結的,但是這樣畢竟還是會對網站造成影響的,那麼怎樣來完全禁止蜘蛛爬取這些連結呢?其實我們可以將這些評論連結轉換成站內連結然後使用 robots.txt 來完全遮蔽這些連結。下面小編就來教大家如何實現評論連結轉為內鏈轉跳的方法。

1 、首先開啟當前 WordPress 主題的 functions.php 檔案,並加入以下程式碼:

add_filter('get_comment_author_link', 'add_redirect_comment_link', 5);
add_filter('comment_text', 'add_redirect_comment_link', 99);
function add_redirect_comment_link($text = ''){
   $text=str_replace('href="',%20'href="'.get_option('home').'/wxd_to.php?v7v3=', $text);
   $text=str_replace("href='", "href='".get_option('home')."/wxd_to.php?v7v3=", $text);
   return $text;
}

2 、在網站根目錄新建一個 wxd_to.php 檔案並寫入以下程式碼:

<html>
<head>
   <meta http-equiv=Content-Type content="text/html;charset=utf-8">
   <title>weixiaoduo.com 轉跳頁</title>
   <meta name="robots" content="noindex,nofollow">
   <meta http-equiv="refresh" content="3;url=<?php%20$url=$_GET['v7v3'];%20echo%20htmlspecialchars($url);?>">
   <style>
   body {background:#F7F7F7}
   p {background:#FFFFCC;border:1px solid #FFCC00;font-size:14px;letter-spacing: 1px;line-height:2;margin:auto;overflow:hidden;padding:15px 30px;width:550px}
   span {color:red;border-bottom:1px solid}
   </style>
</head>
<body>
   <p>
       請稍等!3 秒後轉跳轉至:<span><?php%20$url=$_GET['v7v3'];%20echo%20htmlspecialchars($url);?></span><br><br>
       您也可以 <a href="http://<?php%20$url=$_GET%5B'v7v3'%5D;%20echo%20htmlspecialchars($url);?>"> 點選此處</a> 立即開啟連結, 或者返回 <a href="https://www.weixiaoduo.com/"> 薇曉朵</a> 繼續瀏覽其它內容.
   </p>
</body>
</html>

然後在 robots.txt 檔案中寫入以下程式碼:

Disallow: /wxd_to.php?*

其實不修改 robots.txt 也沒關係,因為小編在轉跳頁面已經用

<meta name="robots" content="noindex,nofollow">

標籤將頁面進行了抓取遮蔽。