本篇采用的是利用wp内在的钩子实现的。需要用到两个钩子:
一、对评论者链接重新定义,加密并且添加nofollow
// 重定义评论者链接-加密并添加nofollow
function redefine_comment_author_link() {
$encodeurl = get_comment_author_url( $comment_ID );
$url = get_option('home').'/jv?url=' . base64_encode($encodeurl);//jv?url 自己修改,下面对应修改$_GET['url']中的url
$author = get_comment_author( $comment_ID );
if ( empty( $encodeurl ) || 'http://' == $encodeurl )
return $author;
else
return "<a href='$url' rel='external nofollow' class='url'>$author</a>";
}
add_filter('get_comment_author_link', 'redefine_comment_author_link');
二、解码上面用base64加密的评论用户外链实施跳转
function redirect_comment_link(){ // 重定向评论者链接
$redirect = base64_decode($_GET['url']); // 解密GET获得的加密链接部分
if($redirect){
if(strpos($_SERVER['HTTP_REFERER'],get_option('home')) !== false){
header("Location: $redirect");
exit;
} else {
header("Location: http://www.inlojv.com/");//修改成你自己的地址
exit;
}
}
}
add_action('init', 'redirect_comment_link');
最后也别忘了修改一下robots.txt,添加 Disallow: /jv?* ,到此已经全部完工。
goto/xxxxx 形式的链接跳转
另一种不采用$_GET方式跳转的方法,转换成 http://xxx.com/goto/base64加密码 的形式。
分成两步:
一、先用正则以及wp的钩子修改外链形式为 /goto/xxxxx
二、然后解密xxxx部分,用template_redirect钩子和wp_redirect函数进行302跳转
代码如下:
function convert_to_internal_links($content){
preg_match_all('/shref=('|")(http[^'"#]*?)('|")([s]?)/',$content,$matches);
if($matches){
foreach($matches[2] as $val){
if(strpos($val,home_url())===false){
$rep = $matches[1][0].$val.$matches[3][0];
$new = '"'.home_url().'/goto/'.base64_encode($val).'" target="_blank"';
$content = str_replace("$rep","$new",$content);
}
}
}
return $content;
}
add_filter('the_content','convert_to_internal_links',99); // 文章正文外链转换
add_filter('comment_text','convert_to_internal_links',99); // 评论内容的链接转换
add_filter('get_comment_author_link','convert_to_internal_links',99); // 访客的链接转换
function inlo_redirect() {
$baseurl = 'goto';
$request = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$hop_base = trAIlingslashit(trailingslashit(home_url()).$baseurl); // 删除末尾的 斜杠/ 符号
if (substr($request,0,strlen($hop_base)) != $hop_base) return false;
$hop_key = str_ireplace($hop_base, '', $request);
if(substr($hop_key, -1) == '/')$hop_key = substr($hop_key, 0, -1);
if (!empty($hop_key)) {
$url = base64_decode($hop_key);
wp_redirect( $url, 302 );
exit;
}
}
add_action('template_redirect','inlo_redirect');
