WordPress設定評論連結重新導向跳轉
首先:在主題目錄下的函數模板<functions.php>的最後?>位置添加如下代碼:
| 代碼如下 |
複製代碼 |
//comments link redirect // 以下是我添加的wordpress設定評論連結重新導向跳轉
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'];
$host = $_SERVER['HTTP_HOST'];
if($redirect){
if(strpos($_SERVER['HTTP_REFERER'],get_option('home')) !== false){
header("Location: $redirect#form:$host");
exit;
}
else {
header("Location: $redirect#form:$host");
exit;
}
}
} |
//以上是我添加的wordpress設定評論連結重新導向跳轉
這樣URL跳轉是基本完成了。接下來就是要實現超連結在新視窗中開啟了。開啟wp-includes目錄下的comment-template.php檔案,到第147行左右的get_comment_author_link()函數(也就是function get_comment_author_link( $comment_ID = 0 )),在第155行else $return 這列標籤裡加入target=‘_blank’屬性,修改後上傳即可。完整的代碼貼上來:
| 代碼如下 |
複製代碼 |
|
function get_comment_author_link( $comment_ID = 0 ) {
/** @todo Only call these functions when they are needed. Include in if... else blocks */
$url = get_comment_author_url( $comment_ID );
$author = get_comment_author( $comment_ID );
if ( empty( $url ) || 'http://' == $url )
$return = $author;
else
$return = "<a href='$url' rel='external nofollow' target=‘_blank’ class='url'>$author</a>";
return apply_filters('get_comment_author_link', $return);
} |
根據下面圖裡在第一句的nofollow後面增加一個target=”_blank”。
設定評論連結Nofollow屬性
wordpress的評論連結添加Nofollow屬性和設定URL跳轉,來避免垃圾評論,垃圾連結對自己部落格品質的影響。你可以通過下面在 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="', '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: http://www.111cn.net/"); //這個連結換成你自己網站
exit;
}
|
以上就是怎樣給wordpress的評論連結添加Nofollow屬性和設定評論連結重新導向跳轉的方法