现实方法如下
新建一个go.
//by https://www.shknn.com
$t_url = preg_replace('/^url=(.*)$/i','$1',$_SERVER["QUERY_STRING"]);
if(!empty($t_url)) {
preg_match('/(http|https):\/\//',$t_url,$matches);
if($matches){
$url=$t_url;
$title='页面加载中,请稍候...';
} else {
preg_match('/\./i',$t_url,$matche);
if($matche){
$url='http://'.$t_url;
$title='页面加载中,请稍候...';
} else {
$url='//www.alittletiger.com/';
$title='参数错误,正在返回首页...';
}
}
} else {
$title='参数缺失,正在返回首页...';
$url='//www.alittletiger.com/';
}
?>
<?php echo $title;?><?php echo $title;?>html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}body{background:#3498db;}#loader-container{width:188px;height:188px;color:white;margin:0 auto;position:absolute;top:50%;left:50%;margin-right:-50%;transform:translate(-50%,-50%);border:5px solid #3498db;border-radius:50%;-webkit-animation:borderScale 1s infinite ease-in-out;animation:borderScale 1s infinite ease-in-out;}#loadingText{font-family:'Raleway',sans-serif;font-size:1.4em;position:absolute;top:50%;left:50%;margin-right:-50%;transform:translate(-50%,-50%);}@-webkit-keyframes borderScale{0%{border:5px solid white;}50%{border:25px solid #3498db;}100%{border:5px solid white;}}@keyframes borderScale{0%{border:5px solid white;}50%{border:25px solid #3498db;}100%{border:5px solid white;}}
页面加载中...
然后保存,可用的外链跳转形式为: {本站地址}/go.php?{外链地址}? 在添加外链的时候,只要给外链加上统一的跳转前缀:http://网站地址/go.php?? 即可。
重写外链
做好了跳转页面,将如下 php 代码添加到
A. 替换文章内容中的外链
在主题目录下的functions.php新增如下函数,即可将文章中的外链替换为go跳转的形式:
//给外部链接加上跳转
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($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)){
$content=str_replace("href=\"$val\"", "href=\"".home_url()."/go.php?url=$val\" ",$content);
}
}
}
return $content;
}
B. 替换评论者的链接
在主题目录下的functions.php查找是否存在修改评论链接为新窗口commentauthor函数,如果存在则如下修改第8行,将$url修改为/go/?url=$url,其实就是在前面新增一个go跳转即可,相同的道理!//评论链接新窗口
add_filter('get_comment_author_link',%20'add_redirect_comment_link',%205);
add_filter('comment_text',%20'add_redirect_comment_link',%2099);
function add_redirect_comment_link($text = ''){
$text=str_replace('href="',%20'href="'.get_option('home').'/go.php?url=', $text);
return $text;
}
Ps:如果functions里面没有这个评论新窗口的函数,请自己找到评论列表输出的代码位置(可能在comments.php),然后参考修改即可(国内主题一般都会有个评论新窗口函数,自己仔细找找看)!
只要参考上述代码,修改一下替换后的链接形式即可。部署后,刷新前台文章或评论,就能看到效果了。
此方法博主未测试,如在使用过程中遇到问题请在下方留言或都查看原文。
赞赏