分享一个WordPress外链跳转教程,兼容知更鸟暗箱下载和文章索引

建站笔记1年前 (2023)发布 小萝卜头
324 0 0

张戈博客很久很久之前转载过一篇关于博客外链跳转的方法(相关文章),后来安装了 Anylink 插件也就没有用到。近来清点插件时,我将 Anylnk 给淘汰了,换成了网上找到的给外链添加 nofollow 的代码。
一、原版代码

//给外部链接加上跳转,将此代码添加到 wordpress 主题目录的 functions.php 里面即可
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content){
preg_match_all('/href="https://zhang.ge/(.*?)"https://zhang.ge/",$content,$matches);
if($matches){
foreach($matches[1] as $val){
if( strpos($val,home_url())===false){
$content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\" ",$content);
}
}
}
return $content;
}

代码原理也挺简单,通过匹配文章 content 内容,若发现存在外链,就给这个外链 a 标签加上 rel=”nofollow”属性。
这个代码常规博客确实可以用,但有可能导致一些特殊链接失效,比如这个代码很可能会让知更鸟主题的下载按钮无法弹出或者文章索引损坏,应该是替换过程中被破坏掉了。
针对这个问题,我对代码进行了第一个改进:
二、改进代码

//给外部链接加上跳转
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content)
{
preg_match_all('/<a(.*?)href="https://zhang.ge/(.*?)"(.*?)>/',$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/?url=$val\" ",$content);
}
}
}
return $content;
}

①、加入了对常规 http://或 https://开头的链接判断,就能有效的绕过知更鸟主题的一些特殊链接,就因为这些特殊链接都不带 http://:
strpos($val,’://’)!==false
②、2014-11-26 补充:加入了对 完整 a 标签 判断:
preg_match_all(‘/<a(.*?)href=”https://zhang.ge/(.*?)”(.*?)>/’,$content,$matches);

foreach($matches[2] as $val){
主要目的是为了绕过高亮代码中的一些外部链接,因为在代码中出现自己博客的跳转形式可能会破坏代码功能,比如博客分享的《百度收录查询和显示代码》,若不过滤的话,其中的百度链接将会被加上跳转,对使用者带来困惑:

③、2015-08-31 补充:加入对外链图片的过滤
有朋友反馈使用后,外链图片也跳转了,导致暗箱跪了。确实会有这个情况,所以在代码中加入了图片的过滤:
!preg_match(‘/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i’,$val)
偶然翻看博客旧文章,又看到了以前分享的跳转办法,觉得可以和现在的外链处理代码结合一下,或许可以相得益彰,更加完善!
详细部署步骤如下:
三、最终代码
①、新增跳转
根据以前分享的方法,在网站根目录新增一个文件夹,命名为 go,并在 go 文件夹下新增一个 index.php,内容如下:
<?php
$url = $_GET[‘url’];
Header(“Location:$url”);
?>
现在用浏览器访问 http://域名/go/?url=外链就能实现跳转了, 比如访问: https://zhang.ge/go/?url=http://www.baidu.com 就能跳转到百度了。
②、新增 robots 规则
为了防止搜索引擎抓取这种跳转链接,我们可以在 robots.txt 里面新增禁止抓取/go 的规则:
…以上内容略…

Disallow: /go

…以下内容略…
③、重写外链
i. 替换文章内容中的外链
在主题目录下的 functions.php 新增如下函数,即可将文章中的外链替换为 go 跳转的形式:

//给外部链接加上跳转
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content)
{
preg_match_all('/<a(.*?)href="https://zhang.ge/(.*?)"(.*?)>/',$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/?url=$val\" ",$content);
}
}
}
return $content;
}

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

//评论链接新窗口
function commentauthor($comment_ID = 0) {
$url = get_comment_author_url( $comment_ID );
$author = get_comment_author( $comment_ID );
if ( empty( $url ) || 'http://' == $url )
echo $author;
else
echo "<a href="".home_url()."/go/?url=$url" rel="external nofollow" target="_blank" class="url">$author</a>";
}

Ps:如果 functions 里面没有这个评论新窗口的函数,请自己找到评论列表输出的代码位置(可能在 comments.php),然后参考修改即可(国内主题一般都会有个评论新窗口函数,自己仔细找找看)!
iii. 知更鸟副标题转载来源链接跳转
其实知更鸟的转载来源链接本身就有 nofollow,不过强迫症嘛,还是继续修改下:
打开知更鸟主题目录下的 includes 文件夹,找到 source.php 文件,如下修改$reprinted 所在行即可:
echo ‘&#8260; 转载:’.'<a href=”‘.home_url().’/go/?url=”.$reprinted.”” rel=”external nofollow” target=”_blank”>原文链接</a>’;

看到这里,相信你应该能轻松领悟方法了吧?就是在外链链接之前加上【http://博客域名/go/?url=】即可!需要修改博客哪个位置的外链,只要找到该位置对应的主题模板,然后参考上述代码修改即可!
2015-07-16 最新补充:
有不少朋友留言要我分享张戈博客目前在用的跳转页面代码,好吧,那就分享一下吧!
go.php 的代码如下:

<?php
//$t_url=$_GET['url']; //此代码无法支持带请求参数的目的地址,已弃用!
$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="https://zhang.ge/";
$title="参数错误,正在返回首页...";
}
}
} else {
$title="参数缺失,正在返回首页...";
$url="https://zhang.ge/";
}
?>
<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>

也可以保存为 index.php 文件,然后上传到网站根目录下的 go 文件夹(没有 go 文件夹就新建一个),实现 https://zhang.ge/go/?url=https://zhang.ge/ 的跳转形式。
更简单的评论者链接跳转:如果想要让评论者链接也弄成这种跳转形式,只要在 WordPress 主题目录下 functions.php 中插入如下代码即可:

//评论者链接重定向
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/?url=", $text);
return $text;
}

记得代码中的“/go/?url=”需要根据实际使用的跳转形式修改即可!

2016-02-16 最新补充:张戈博客已分享最新跳转代码,更安全效率==>传送门

收录于{张戈博客} 原文链接原文链接

© 版权声明

相关文章

暂无评论

暂无评论...