一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress中设置评论链接重定向跳转且加Nofollow属性

时间:2013-09-01 编辑:简简单单 来源:一聚教程网

WordPress设置评论链接重定向跳转

首先:在主题目录下的函数模板的最后?>位置添加如下代码:

 代码如下 复制代码
//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 = "$author";
 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.111com.net/"); //这个链接换成你自己网站
exit;
}

以上就是怎样给wordpress的评论链接添加Nofollow属性和设置评论链接重定向跳转的方法

热门栏目