如何免插件实现WordPress外链转内链呢?开始看到很多博客的相关方法,如何实现WordPress无需插件外链转内链,
不过大多数要么就是加入了 base64 将源链接加密,很复杂,或者还有加上了 nofollow,然蜘蛛不在爬行,
那些教程方法都很复杂,这里无作为分享一下优化了的代码,直接实现WordPress无需插件外链转内链,并且带base64 将源链接加密,
以及给外链加上nofollow属性,不过有些但恐怕蜘蛛还是能爬行,所以大家直接robots禁止所有蜘蛛爬行 /go?url 目录吧,
下面开始教程:
首先,在你当前使用主题的 functions.php 中加入以下代码:
add_filter('the_content','baezone_the_go_url',999); function baezone_the_go_url($content){ preg_match_all('/href="(.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "href=\"" . get_bloginfo('wpurl'). "/go?url=" .base64_encode($val). "\"",$content); } } return $content; }
然后在网站根目录下新建个名为 go 的文件夹,在其中写个 index.php 的文件,内容如下(请保存为UTF-8无BOM格式): 推荐大家用notepad++编辑好再上传,免得出其他错误。
<?php $url = $_GET['url']; $url = base64_decode($url); ?> <html> <head> <meta charset=utf-8 /> <meta name="robots" content="nofollow"> <meta http-equiv="refresh" content="0.1;url=<?php echo $url; ?>"> <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:41.5%;left:47%;margin:16px 0 0 35px;color:#BBB;letter-spacing:1px;font-weight:700;font-size:9px;font-family:Arial}.spinner{position:absolute;top:40%;left:45%;display:block;margin:0;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>
以上代码为美化版本,跳转效果更加美观,代码中已经用 base64 将源链接加密,并且加上了 nofollow,
如果只是单纯想实现外链转内链这一功能请看下方:
把以下代码加入主题根目录下的functions.php文件中(一般是在 ?> 之前)
add_filter('the_content','dmeng_the_go_url',999); function dmeng_the_go_url($content){ preg_match_all('/href="(.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,home_url())===false ) $content=str_replace("href="$val"", "href="" . get_bloginfo('template_url'). "/go?url=" .base64_encode($val). """,$content); } } return $content; }
然后在主题根目录下新建一个 go 目录,并在该目录下新建一个内容如下的index.php
<?php $url = $_GET['url']; $url = base64_decode($url); header("Location:" . $url); exit; ?>
即可单纯的实现WordPress内链转外链了。
2016年7月12日23:45:15,补充一个新的方式,也是无作为目前自用的方式:
首先,在你当前使用的主题的 functions.php 中加入以下代码:
// 自动给文章的外部链接添加nofollow属性 add_filter('the_content','web589_the_content_nofollow',999); function web589_the_content_nofollow($content){ preg_match_all('/href="(http.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "rel=\"nofollow\" href=\"" . get_bloginfo('wpurl'). "/link?url=" .base64_encode($val). "\"",$content); } } return $content; } // 自动给文章的外部链接添加nofollow属性
这里对上面自动代码做一个完善和补充(2019年10月27日01:26:16),因为上面代码比较单一,会屏蔽很多链接,比如图片无法暗箱的问题。
完善代码如下:
// 自动给文章的外部链接添加nofollow属性(纠正外链图片暗箱功能失效by无作为) add_filter('the_content','web589_the_content_nofollow',999); function web589_the_content_nofollow($content){ preg_match_all('/href="(http.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)) $content=str_replace("href=\"$val\"", "rel=\"nofollow\" href=\"" . get_bloginfo('wpurl'). "/link?url=" .base64_encode($val). "\"",$content); } } return $content; } // 自动给文章的外部链接添加nofollow属性
其中添加了对外链图片的过滤!preg_match(‘/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i’,$val)
&&为并列语句。
方法转自“https://zhang.ge/4683.html”
然后在网站根目录下新建个 link 的文件夹,在其中写个 index.php 的文件,内容如下(请保存为UTF-8):
<?php $url = $_GET['url']; $a = ''; if( $a==$url ) { $b = "http://www.w1234.net/"; // echo 'true'; } else { $b = $url; $b = base64_decode($b); } //Template Name:链接跳转(有过度) ?> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <meta http-equiv="refresh" content="0.1;url=<?php echo $b; ?>"> <title>正在跳转....</title> </head> <body> </body> </html>
即可完美实现WordPress无需插件外链转内链。
历史上的今天:
- 2019: Webmshare|可以上传视频分享观看的网站(0)
- 2019: 福利吧26|深田绘美(E罩杯)(5)
- 2019: 快递抵扣券集合分享(0)
- 2019: 李庆老师《唐诗100首倒背如流》课程|快速背唐诗(0)
- 2019: 0撸移动王卡送236元话费(已成功上车,附上免套路全过程)(18)
你的方法并不会跳转。。。。