无需插件为WordPress的文章外链转内链且进行Base64加密

此教程来自于pc6a,站长做了精简和修改,它可以把自动外链转换成“你的网址/go/”。非常方便,而且是php自动转换的,不存储数据,不需要占用服务器的空间。绝大部分代码都是直接引用。小白的教程更适合跟我一样的纯小白使用。

一、添加加密代码

首先打开主题文件夹下functions.php文件,按需添加以下代码

1、文章外链转内链:

//文章外链跳转伪静态版
add_filter('the_content','link_jump',999);
function link_jump($content){
preg_match_all('//',$content,$matches);
if($matches){
foreach($matches[2] as $val){
    if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('//.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val) && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl)://///i',$val)){
    $content=str_replace("href=/"$val/"", "href=/"".home_url()."/go/".base64_encode($val)."/" rel=/"nofollow/" target=/"_blank/"",$content);
    }
}
}
return $content;
}

2、评论外链转内链:

//评论者链接重定向
function commentauthor($comment_ID = 0) {
$url    = get_comment_author_url( $comment_ID );
$author = get_comment_author( $comment_ID );
if ( empty( $url ) || 'http://' == $url ) {
echo $author;
} else {
if (!preg_match(home_url(),$url)) {
    echo "$author";
} else {
    echo "$author";
}
}   
}

二、创建go跳转文件

在网站根目录创建go.html文件,在文件中写入以下代码:





//base64加密解密函数
var base64EncodeChars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var base64DecodeChars=new Array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);function base64encode(str){var out,i,len;var c1,c2,c3;len=str.length;i=0;out="";while(i>2);out+=base64EncodeChars.charAt((c1&3)2);out+=base64EncodeChars.charAt(((c1&3)4));out+=base64EncodeChars.charAt((c2&15)2);out+=base64EncodeChars.charAt(((c1&3)4));out+=base64EncodeChars.charAt(((c2&15)6));out+=base64EncodeChars.charAt(c3&63)}return out}function base64decode(str){var c1,c2,c3,c4;var i,len,out;len=str.length;i=0;out="";while(i>6)&31));out+=String.fromCharCode(128|((c>>0)&63))}}}return out}function utf8to16(str){var out,i,len,c;var char2,char3;out="";len=str.length;i=0;while(i>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:out+=str.charAt(i-1);break;case 12:case 13:char2=str.charCodeAt(i++);out+=String.fromCharCode(((c&31)