^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

分享陌小雨博客导航外链跳转样式及xiu主题评论用户名外链修改办法

分享陌小雨博客导航外链跳转样式及xiu主题评论用户名外链修改办法

之前给大家分享过一款插件:anylink,这是陌小雨博客一直在用的一款插件,主要特点是自动,便捷,可以把 wordpress 文章和评论中的代码都替换成以自己的域名开头的短网址。前段时间用 wordpress 仿了一个站点,并添加了其他网站程序特有的最近来访功能,再融入自己对导航站的一些理解,做成了一个简单的导航,因为首页分享了很多优秀的网址,所以很有必要添加一个跳转。

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

下面分享不用插件的实现办法,以下代码来至张戈博客

<?php
$t_url=$_GET['url'];
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://dedewp.com/';
            $title='参数错误,正在返回首页...';
        }
    }
} else {
    $title='参数缺失,正在返回首页...';
    $url='https://dedewp.com/';
}
?>
<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(255,0,0,1);-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 文件夹下。

要实现效果,只需要在你的外链网址面前添加 https://dedewp.com/go/url=即可,效果演示:陌小雨博客导航

如何替换老文章中所有的外链呢?如何在编辑文章时便捷的添加呢?

1、代码法(适合新老文章)

//给外部链接加上跳转
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content)
{
    preg_match_all('/<a(.*)href="(.*)"(.*)>/',$content,$matches);
    if($matches && !is_page('about')){
        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;
}

将上述代码添加到主题 functions,php 文件中即可,如果你是 wordpress 新手的话,推荐你看一下这篇文章:添加代码到 functions,php 常见的错误

2、在 xiu 主题如何实现和 anylink 插件一样,替换评论中的用户名的超链接呢?这是陌小雨博客思考的问题。

在主题文件 functions.xiu.php 文件中查找 get_comment_author_link(),然后将那行 echo 语句修改为:

 echo '<a href="'.home_url().'/go/url='.get_comment_author_url().'" rel="external nofollow" target="_blank"><span class="c-author">'.trim(get_comment($parent_id)->comment_author).'</span></a>';

其他主题的话,先在 comment.php 找到 callback 后面的函数,然后在主题文件 functions.php 中搜素该函数,再对照上面的代码修改即可。

2016 年 4 月 7 日更新:代码优化,主要加入 Base64 加密,两种方法共存,避免修改以前设置过的链接。

(1)新建 goto.php 文件,放入网站根目录,代码如下

(2)、替换上文之前加入到 functions.php 中的代码the_content_nofollow函数

//文章外链跳转伪静态版
add_filter('the_content','link_jump',999);
function link_jump($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) && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val)){
	        $content=str_replace("href=\"$val\"", "href=\"".home_url()."/go/".base64_encode($val)."\" rel=\"nofollow\"",$content);
			}
		}
	}
	return $content;
}

(3)在.htaccess顶部添加代码:

RewriteEngine On
RewriteRule ^goto/(.*)$ /goto.php?url=$1 [L]

(4)对应 xiu 主题评论用户链接修改:

同之前的修改办法,修改相应的 echo 语句:

echo '<a href="'.home_url().'/goto/'.base64_encode(get_comment_author_url()).'" rel="external nofollow" target="_blank"><span class="c-author">'.trim(get_comment($parent_id)->comment_author).'</span></a>';

赠人玫瑰,手有余香。