wordpress中設定評論連結重新導向跳轉且加Nofollow屬性

來源:互聯網
上載者:User

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屬性和設定評論連結重新導向跳轉的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.