wordpress中设置评论链接重定向跳转且加Nofollow属性
现在推广人员无处不在我们的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 ( empty 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.phpfensi.com/" ); //这个链接换成你自己网站 exit ; }以上就是怎样给wordpress的评论链接添加Nofollow属性和设置评论链接重定向跳转的方法.
查看更多关于wordpress中设置评论链接重定向跳转且加Nofollow属性的详细内容...