> 转载 > wordpress网站怎么设置评论链接重定向跳转

wordpress网站怎么设置评论链接重定向跳转

今天谢谢站长朋友提醒,噜噜一直不放心的wordpress网站评论框,除了nofollow保护外还加上了链接重定向跳转。之前也是懵懵懂懂的,因为觉得那些评论者的超链接被nofollow掉,应该可以了吧。但是查看收录的时候还是发现了百度抓取了某某页面的评论。所以说nofollow也不是百分百保险啊。

细心的朋友可以去看看噜噜的wordpress博客,随便打开一篇文章,鼠标放在任一一个评论者上面,经过定向以后的url地址就成了http://www.yilulu.cn/?r=评论者网站,当你点击这个链接,就会自动跳转到对方的网站。这样做的好处,可以更加有效的减少网页权重的分散。

那么,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”。

链接跳转

最后,建议在robots文件中增加一行:Disallow: /?r=* 告诉搜索引擎不要抓取此页面。

原文地址:http://www.yilulu.cn/wordpress-comment-url.html

典型的夜猫子,失眠者。爱好LOL、徒步、旅行、拍照、音乐、小球。E-mail:[email protected]

网友评论2

  1. 沙发
    wordpress大神:

    wordpress网站怎么设置评论链接重定向跳转

    2013-04-12 上午11:55 [回复]
    • 上梅镇:

      文章内不是写了吗。大哥

      2013-04-12 上午11:55 [回复]

发表评论

表情
还能输入210个字

了解 李英杰害虫 的更多信息

立即订阅以继续阅读并访问完整档案。

Continue reading