WordPress实现外链Go跳转效果

防止外链影响自己WordPress博客的权重,导致搜索引擎降权,可以给网站加一个外链Go跳转效果。本文的内容介绍如何使用代码实现外链跳转。包括外链跳转的页面样式以及自动识别博文中的外链。

设计外链跳转页面

在WordPress的根目录(与wp-admin文件夹同级)下新建一个go文件夹,在里面新建一个go.php:

<?php
/**
 * Created by PhpStorm.
 * User: Flyzy
 * Date: 2018/1/14
 * Time: 19:59
 */
$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://www.flyzy2005.com';
			$title = '参数错误,正在返回首页...';
		}
	}
} else {
	$title = '参数缺失,正在返回首页...';
	$url   = 'https://www.flyzy2005.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(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中加入如下代码,自动进行外链替换:

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/go.php?url=$val\" ",$content);
			}
		}
	}
	return $content;
}

至此,你已经完成了博文中外链自动增加跳转效果的功能~