WordPress免插件外链转内链

WordPress免插件外链转内链 | JUST FOR FUN
以前写过一篇文章介绍了一些wordpress外链转内链插件。

WordPress外链跳转插件


今天就记录几种不需安插件的方法。
1:直接实现WordPress无需插件外链转内链,并且 将源链接base64加密
首先,在你当前使用的主题的 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格式)。

<?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。
2:直接实现外链转内链
把以下代码加入主题根目录下的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;
?>

即可。
3:首先,在你当前使用的主题的 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属性

然后在网站根目录下新建个 link 的文件夹,在其中写个 index.php 的文件,内容如下:

<?php
$url = $_GET['url'];
$a = '';
if( $a==$url ) {
 $b = "http://www.wuzuowei.com/";
// 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>

 

WP技巧代码·功能

DirectAdmin(DA)面板开启网页GZIP压缩

2016-7-14 11:07:51

WP技巧

复制网页内容自动添加版权信息

2016-8-16 15:34:40

个人中心
今日签到
有新私信 私信列表
搜索