将WP博文评论外部链接优化为内链gourl

WordPress文章评论的链接转化成内部链接,比如把http://goo.gl/rehV3变成:http://www.searu.org/go/go.php?url=http://goo.gl/rehV3

a、把以下代码加到主题中的function.php中:

//gourl.begin
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="', 'target="_blank" href="'.get_option('home').'/go/go.php?url=', $text );
$text=str_replace("href='", "target='_blank'href='".get_option('home')."/go/go.php?url=", $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.searu.org");
exit;
}
}
}
//gourl.end

b、在站点根目录新建“./go/go.php”,go.php的源码:

	


";
  echo   "location='$url';";
  echo   "";
?>


页面跳转中... 请稍等...


Ps.如果你主题的functions.php文件中本来就有

那么你只要找到类似代码












将代码中的

改成:

/go/go.php?url=

嗯,其实这么做,我知道不咋滴友好,不过先搞着看看吧,其实对于博友交流影响不是太大吧……先试试试试。

摘录【WordPress】折腾HotNewsPro – 外部链接内部化 | 西德欧的实验室