当前位置:首页>WordPress>WordPress教程>WordPress博客添加go跳转效果的详细教程(附跳转代码)

WordPress博客添加go跳转效果的详细教程(附跳转代码)

经常看到一些博客点击外链跳转到其他网站上的时候都会有一个跳转页面,很是美观。据说既有利于SEO,又可保护站点避免权重流失,不过个人只是觉得好看、高逼格便加上了 。

WordPress博客添加go跳转效果的详细教程(附跳转代码)

首先需要说明的是代码来自 AE博客 与 张戈博客 ,代码版权归原作者所有,这里仅介绍使用方法。实现 Go 页面跳转的简单原理是:以往我们跳转到其他网站是直接访问该网站的链接;使用 Go 页面跳转后,我们先访问本地的 Go 页面(也就是下面 go 文件夹下的 index.php 或直接 go.php文件)然后再跳转到相应的外链网站。

代码方法

代码部署

①、新建一个go.php文件,放置到wordpress的根目录下,在go.php里面输入以下代码

这是知更鸟Begin主题集成的go.php代码

<?php
// https://www.ciyol.cn/
$t_url = preg_replace('/^url=(.*)$/i','$1',$_SERVER["QUERY_STRING"]);
if(!emptyempty($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='https://www.ciyol.cn/';
            $title='参数错误,正在返回首页...';
        }
    }
} else {
    $title='参数缺失,正在返回首页...';
    $url='https://www.ciyol.cn/';
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="refresh" content="1;url='<?php echo $url;?>';">
<title><?php echo $title;?></title>
<style>
body{background:#000}.loading{-webkit-animation:fadein 2s;-moz-animation:fadein 2s;-o-animation:fadein 2s;animation:fadein 2s}@-moz-keyframes fadein{from{opacity:0}to{opacity:1}}@-webkit-keyframes fadein{from{opacity:0}to{opacity:1}}@-o-keyframes fadein{from{opacity:0}to{opacity:1}}@keyframes fadein{from{opacity:0}to{opacity:1}}.spinner-wrapper{position:absolute;top:0;left:0;z-index:300;height:100%;min-width:100%;min-height:100%;background:rgba(255,255,255,0.93)}.spinner-text{position:absolute;top:50%;left:50%;margin-left:-90px;margin-top: 2px;color:#BBB;letter-spacing:1px;font-weight:700;font-size:36px;font-family:Arial}.spinner{position:absolute;top:50%;left:50%;display:block;margin-left:-160px;width:1px;height:1px;border:25px solid rgba(100,100,100,0.2);-webkit-border-radius:50px;-moz-border-radius:50px;border-radius:50px;border-left-color:transparent;border-right-color:transparent;-webkit-animation:spin 1.5s infinite;-moz-animation:spin 1.5s infinite;animation:spin 1.5s infinite}@-webkit-keyframes spin{0%,100%{-webkit-transform:rotate(0deg) scale(1)}50%{-webkit-transform:rotate(720deg) scale(0.6)}}@-moz-keyframes spin{0%,100%{-moz-transform:rotate(0deg) scale(1)}50%{-moz-transform:rotate(720deg) scale(0.6)}}@-o-keyframes spin{0%,100%{-o-transform:rotate(0deg) scale(1)}50%{-o-transform:rotate(720deg) scale(0.6)}}@keyframes spin{0%,100%{transform:rotate(0deg) scale(1)}50%{transform:rotate(720deg) scale(0.6)}}
</style>
</head>
<body>
<div class="loading">
  <div class="spinner-wrapper">
    <span class="spinner-text">页面加载中,请稍候...</span>
    <span class="spinner"></span>
  </div>
</div>
</body>
</html>

使用方法

②、保存,则外链跳转形式为: {本站地址}/go.php?url={外链地址}

使用:在添加外链的时候,只要给外链加上统一的跳转前缀:http://网站地址/go.php?url= 即可。


外链方法的使用是需要在手动添加外链的,同时跳转样式可以自行修改。但是这样每次添加外链都手动添加的话太麻烦,使用下面的代码既可以实现外链自动添加http://网站地址/go.php?url=

A、文章内外链添加go跳转

在主题目录下的functions.php新增如下函数,即可将文章中的外链替换为go跳转的形式:

//文章内外链添加go跳转
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($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)){
        $content=str_replace("href=\"$val\"", "href=\"".home_url()."/go.php?url=$val\" ",$content);
    }
    }
    }
    return $content;
}

B、评论者链接添加go跳转

在主题目录下的functions.php查找是否存在修改评论链接为新窗口commentauthor函数,如果存在则如下修改第8行,将$url修改为/go.php/?url=$url,其实就是在前面新增一个go跳转即可,相同的道理!

//评论者链接添加go跳转
 add_filter('get_comment_author_link', 'add_redirect_comment_link', 5);
 add_filter('comment_text', 'add_redirect_comment_link', 99);
 function add_redirect_comment_link($text = ''){
    $text=str_replace('href="', 'href="'.get_option('home').'/go.php?url=', $text);
    return $text;
 }

Ps:如果functions里面没有这个评论新窗口的函数,请自己找到评论列表输出的代码位置(可能在comments.php),然后参考修改即可(国内主题一般都会有个评论新窗口函数,自己仔细找找看)!

只要参考上述代码,修改一下替换后的链接形式即可。部署后,刷新前台文章或评论,就能看到效果了。

©版权声明

本站资源大多来自网络,如有侵犯你的权益请联系管理员Ciyol 或给邮箱发送邮件 admin@ciyol.cn 我们会第一时间进行审核删除。站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请在下载24小时内删除!


如果遇到评论下载的文章,评论后刷新页面点击对应的蓝字按钮即可跳转到下载页面本站资源少部分采用7z压缩,为防止有人压缩软件不支持7z格式,7z解压,建议下载7-zip(点击下载),zip、rar解压,建议下载WinRAR(点击下载)

给TA打赏
共{{data.count}}人
人已打赏
0 条回复A文章作者M管理员
    暂无讨论,说说你的看法吧
购物车
优惠劵
今日签到
有新私信 私信列表
搜索