实现WordPress评论链接重定向

最近一段时间人肉spamer挺多的,搞得你都看不出来到底哪个才是spamer,记得水煮鱼有个插件是可以重定向WordPress评论链接,前段时间冰剑也在问我这个插件叫啥,这插件叫做comments-link-redirect,这插件的作用是:

1.让不遵循nofollow的搜索引擎不会因为垃圾留言而降低你的排名
2.Spammer 即使成功留言,也不会被搜索引擎索引

我以前用过这插件,却发现只能把侧边栏的的最新评论里面的链接给重定向,而文章页里面的链接还是老样子,今天在Google搜索了下,找到了解决问题的办法。

首先启用这个插件,

然后手工在 robots.txt 中添加一行以下代码即可:

Disallow: /?r=*

然后在functions.php找到找到评论用户的调用函数<?php comment_author_link() ?>,在<?php comment_author_link() ?>前添加“http://你的博客地址/?r=”(不含引号),这样就可以了。

我是这样干的,直接把插件代码复制到主题目录下functions.php中

这个插件有两个版本,我用的是水煮鱼的所以代码是

//comments link redirect

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.aenes.com/");

            exit;

        }

    }

}

如果使用了cos-html-cache 静态化插件,那么会导致我爱水煮鱼的 Comments Link Redirect 插件无法正常使用,幸好有hongxiaowan制作的JS版comments link redirect,用JS版本的那代码就是

//comments link redirect

add_filter('get_comment_author_link', 'add_redirect_comment_author_link', 5);

add_filter('comment_text', 'add_redirect_comment_text', 99);

function add_redirect_comment_author_link($text = ''){

$text=str_replace("href=",'href="javascript:window.location=',ereg_replace('href=\'[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]\'',"\\0\"", $text));return $text;

}

function add_redirect_comment_text($text = ''){

$text=str_replace("href=","href='javascript:window.location=",ereg_replace('href=\"[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]\"',"\\0'", $text));return $text;

}

JS版本的不用修改robots.txt
如果你不想折腾,那就直接下载插件启用就ok
水煮鱼版本:点击下载
JS版本:点击下载

欢迎回来, 请订阅本站Feed获取最新文章,感谢你的来访。欢迎到这里参加本站的小调查。

  1. 2009年10月21日 13:14 | #1

    bo也有人搞出来了个
    但是我没有用
    囧……
    要修改源程序

  2. 2009年10月21日 14:02 | #4

    你说的那代码插入到哪里啊??

    • 2009年10月21日 14:22 | #5

      你先看看启用插件行不行~
      不行就在functions.php中加点东西~
      加什么上面写了的

  3. 2009年10月22日 10:48 | #6

    跟我用的一个样,只是你的代码好长。

  4. 2009年10月29日 09:29 | #7

    老T,你这个试过没有?好像评论者的主页打不开啊。

    • 2009年10月29日 09:45 | #8

      我是直接把代码复制到functions.php的~没有用插件~目前一切使用正常~

  5. 2009年10月29日 10:00 | #9

    有的打得开,有的打不开,奇了,难道是网络问题?

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">