現在推廣人員無處不在我們的Wordpress博客他們都不放過,下面我來介紹在博客評論中我們加上重定向跳轉且加Nofollow屬性的方法.
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 ( emptyempty( $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.49028c.com/"); //這個鏈接換成你自己網站
- exit;
- }
以上就是怎樣給wordpress的評論鏈接添加Nofollow屬性和設置評論鏈接重定向跳轉的方法.
新聞熱點
疑難解答
圖片精選