备忘录之Wordpress【七】文章外链转内链并转入跳转页面

折腾了两年的Wordpress,不准备再折腾了,把我所收集和使用的Wordpress代码做个总结吧!

文章外链转内链并转入跳转页面,需要两段代码:

Function代码

// 文章外链转内链并转入跳转页面,by www.yuweiji.com
add_filter('the_content','goto_url',999);
function goto_url($content){
    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$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\"", "rel=\"nofollow\" target=\"_blank\" href=\"" . get_bloginfo('home'). "/goto?url=" .base64_encode($val). "\"",$content);
            }
        }
    }
    return $content;
}
// 下载外链跳转,by www.yuweiji.com
function links_nofollow($url) {
    if(strpos($url,'://')!==false && strpos($url,'echo.net')===false && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$url)) {
    $url = str_replace($url, home_url()."/goto?url=".base64_encode($url),$url);
         }
    return $url;
}

页面模版代码

<?php
/*
Template Name: 外链跳转
Template Athor: www.yuweiji.com
*/

?>
<?php
$home = "https://www.yuweiji.com";; //修改成自己的网址
$name = "WECHO"; //修改成自己的网站名
function be_referer($home,$name) {

echo '&lt;!DOCTYPE html PUBLIC &quot;-//W3C//Dtd XHTML 1.0 transitional//EN&quot; &quot;http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd&quot;&gt;

<html xmlns="http://www.w3.org/1999/xhtml";><head><meta charset="UTF-8" /><meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>无效地址</title></head>

&lt;body&gt;
&lt;div style=&quot;text-align:center;background:#eee;padding:50px 0;margin:0 auto&quot;&gt;
&lt;p style=&quot;font-size:30px&quot;&gt;抱歉,此URL不允许直接复制打开!!!&lt;/p&gt;
&lt;p style=&quot;font-size:30px&quot;&gt;—— 来自&lt;a href=&quot;'.$home.'/&quot;&gt;'.$name.'&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;';
exit();

}
// 判断:若不为跳转链接,则不执行跳转,显示以上信息
if(empty($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper($home)) === false){

be_referer($home,$name);

}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta name="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="Cache-Control" content="no-transform"/>
<meta http-equiv="Cache-Control" content="no-siteapp"/>
<title>温馨提示:您即将离开 - <?php echo $name;?></title>
<style>
@media (max-width: 700px){body {font:12px!important}h2{font-size:18px!important;padding-bottom:10px!important;}.remind_title{font-size:16px!important;}a {font-size:14px!important;}.remind_detail {font-size:14px!important;}#wrapper {width:100%!important;}.c-container {padding: 0px!important;}}
body {font:15px Arial, Lucida Grande, Tahoma, sans-serif;background: #fff;}
h2{font-size:22px;color:#eb5424;text-align:center;padding-bottom: 20px;border-bottom: 5px solid #dfdfdf;}
a {font-size:16px;color:#555;text-decoration: none;}
span {color:#CC0000;}

wrapper {width:700px;margin:0 auto;}

content {background:#fff;margin:10px 0 20px 0;}

.c-container {padding: 20px;}
.remind_icon {float: left;margin-right: 10px;display: inline;width: 32px;height: 32px;background: url('/content/themes/kavico/assets/wp-content/files/prompt.png') no-repeat scroll 0% 0% transparent;background-position:-64px 0px;}
.remind_content {overflow: hidden;}
.remind_title {color:#D68300;margin-bottom: 10px;padding-top: 3px;font-weight: bold;font-size: 20px;font-family: "Microsoft YaHei","lucida Grande",Verdana;}
.remind_detail {line-height:1.5;font-size:15px;color: #535353;}
.safety-url {margin-bottom:15px;padding-bottom: 15px;border-bottom: 1px solid #DFDFDF;word-wrap: break-word;word-break: break-all;}
.c-footer {padding: 10px 15px;background: none repeat scroll 0% 0% #F1F1F1;border-top: 1px solid #BBB;overflow: hidden;text-align:center;}
a.btn_blue {border: 1px solid #0D659B;color: #FFF !important;background:linear-gradient(105deg,#238aca,#0074bc);display: inline-block;padding: 6px 25px;margin: 0px;font-size: 14px;font-weight: bold;text-align: center;border-radius: 3px;}
.c-footer-a2 {margin: 8px 0px 0px 15px;}
</style>
</head>
<body>
<div id="wrapper">

&lt;h2&gt;&lt;a href=&quot;https://www.yuweiji.com&quot; title=&quot;WECHO&quot;&gt;WECHO&lt;/h2&gt;/*修改成您的*/
&lt;div id=&quot;content&quot;&gt;
&lt;div class=&quot;c-container&quot;&gt;
    &lt;span class=&quot;remind_icon&quot;&gt;&lt;/span&gt;
    &lt;div class=&quot;remind_content&quot;&gt;
        &lt;div class=&quot;remind_title&quot;&gt;您将要访问:&lt;/div&gt;
        &lt;div class=&quot;remind_detail&quot;&gt;
            &lt;div class=&quot;safety-url&quot;&gt;
                &lt;span &gt;&lt;a href=&quot;&lt;?php $url=$_GET['url']; echo base64_decode($url);?&gt;&quot; rel=&quot;nofollow&quot; &gt;&lt;?php $url=$_GET['url']; echo base64_decode($url);?&gt;&lt;/a&gt;&lt;/span&gt;&lt;br/&gt;
            &lt;/div&gt;
            &lt;p&gt;该网页不属于余味记,我们无法确认该网页是否安全,它可能包含未知的安全隐患,请注意保护好个人信息! &lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;c-footer&quot;&gt;
    &lt;a href=&quot;&lt;?php $url=$_GET['url']; echo base64_decode($url);?&gt;&quot; rel=&quot;nofollow&quot; class=&quot;c-footer-a1 btn_blue&quot;&gt;继续访问&lt;/a&gt;
    &lt;a href=&quot;https://www.yuweiji.com&quot; class=&quot;c-footer-a2&quot;&gt;返回 WECHO&lt;/a&gt;/*修改成您的*/
&lt;/div&gt;
&lt;/div&gt;

</div>
</body>
</html>

新建goto.php文件,填入页面模版代码,丢入模版文件夹。然后控制后台新建跳转链接页面,内容为空,slug名为goto,选择页面模版,发布即可。

温馨提示:如遇到资源失效,或者浏览更多资源,请访问余味记网盘在线查找,感谢您的访问!欢迎关注微信公众号“迷你日志”获取密码和最新资讯!

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注