最近搞博客,看见别人的博客搞了外链转换成了内链,而且使用的base64加密,由于博主是只是一个搬砖者,网上找了一圈,找到一个比较好用的,分享记录一下!
一、先用正则以及wp的钩子修改外链形式为 /goto/xxxxx
二、然后解密xxxx部分,用template_redirect钩子和wp_redirect函数进行302跳转
添加以下代码到知更鸟主题的functions.php文件即可,代码如下:
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']; //如果启用了https,请将http改为https
$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');
原文链接:https://www.iwzyw.com/2020/05/13/147.html,转载请注明出处。


请先 !