經常在一些朋友的部落格上看到,當滑鼠移動到評論者上時,瀏覽器的狀態列顯示的地址是這樣子的格式:https://www.59iwp.com/?to=http://newrul,網上有人說這樣子每個評論就是一個外鏈,會分散網站權重,雖然不知道是否屬實,但正好前兩天有人諮詢了珞凡關於 Wordpress 評論者連結重定向且在新視窗開啟的方法,所以,今天在這裡就簡單說下實現方法的文章吧。
開啟我們的主題目錄,找到 functions.php,在適當位置加上下面的程式碼 (這個適當位置夠模糊,如果你不確定哪個地方算是適當位置,也可以直接加在 functions.php 最後一個 ?> 之前即可,程式碼如下:
//comments link redirect
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=”‘, ‘href=”‘.get_option(‘home’).’/?r=’, $text);
$text=str_replace(“href='”, “href='”.get_option(‘home’).”/?r=”, $text);
return $text;
}
add_action(‘init’, ‘redirect_comment_link’);
function redirect_comment_link(){
$redirect = $_GET[‘r’];
if($redirect){
if(strpos($_SERVER[‘HTTP_REFERER’],get_option(‘home’)) !== false){
header(“Location: $redirect”);
exit;
}
else {
header(“Location: 你的網址/”);
exit;
}
}
}
以上,儲存並重新整理網頁,完工。