WordPress纯代码实现网站外部链接go跳转效果
我们在写文章的时候很多时候会引用外部的链接,但是在点击的时候容易造成权重的分散,并且跳转的效果也很单调,如果想避免这两种情况的话可以考虑使用go跳转,但是有很多朋友不知道这个go跳转有什么作用以及怎么设置,下面博主就给大家介绍一下。

博主先来说说go跳转有什么作用;第一可以给链接跳转的时候加上一个跳转效果,使点击链接跳转的页面更加的美观,这个效果可以自己通过样式进行修改;第二是可以很好的避免权重的分散,巩固网站自身的权重,对于网站的SEO非常的友好。
有些WordPress主题或者是其他CMS主题会自带go跳转这个功能, 如果你的主题自带这个go跳转功能那就请忽略以下的内容,主题没有带这个go跳转功能朋友可以根据下面的教程来实现网站go跳转效果。
WordPress实现外部链接go跳转
第一步:通过ftp或者服务器在网站根目录创建一个go.php的文件。

第二步:将下面的这段代码复制下来并粘贴到创建好的go.php文件中。
<?php $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='//defcon.cn/'; $title='参数错误,正在返回首页'; } } } else { $title='参数缺失,正在返回首页'; $url='//defcon.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:#3498db} .spinner-text { position:absolute; top:50%; left:50%; margin-left:-200px; margin-top: 2px; color:#fff; letter-spacing:1px; font-weight:700; font-size:36px; font-family:Arial} .spinner { position:absolute; top:50%; left:50%; display:block; margin-left:-270px; 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>
第三步:打开网站主题下面的functions.php文件,注释:当前使用的主题目录。

第四步:将下面的这段代码复制下来并粘贴到functions.php文件中。
//给外部链接加上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; }
第五步:最后回到网站页面刷新一下,即可实现网站外部链接go跳转功能。(跳转格式:https:本站地址/go.php?外部链接) 效果演示:https://www.baidu.com
以上就是利用纯代码实现网站外部链接go跳转功能的方法,当然除了使用代码可以实现这个go跳转功能之外,使用一些WordPress go跳转插件也是可以实现的,如果大家需要插件的话可以在下方留言告诉博主,博主会根据大家的需求整理出一些可以实现网站外部链接go跳转的插件。
版权声明:若无特殊注明,本站文章皆为王金亮博客原创,转载请保留文章出处。
本站原创文章,作者:王金亮,如若转载,请注明出处:https://www.jayseoer.com/1702.html