大家都知道的,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">
標籤將頁面進行了抓取屏蔽。