WordPress 添加文章外链跳转中间页面

凡有一点 SEO 基础的筒子都明白一个道理,即绝不能容忍自己的权重流失!特别是指向那些不必要的站点。所以此前博主把所有的出站链接都干掉了,包括版权信息,可谓干尽所有恶俗事情(最先的 Youth Daily,非本博)。实现方法如下:首先,在你当前使用的主题的 functions.php 中加入以下代码:

// 外链跳转功能
add_filter('the_content', 'add_external_link_redirect', 999);

function add_external_link_redirect($content) {
    // 查找所有 href 属性的值
    preg_match_all('/href="(.*?)"/', $content, $matches);

    if ($matches) {
        // 获取匹配的链接数组
        $links = $matches[1];

        // 排除站内链接和特定 JavaScript 链接
        foreach ($links as $link) {
            if (strpos($link, home_url()) === false && strpos($link, "javascript:void(0)") === false) {
                // 替换外部链接为跳转链接
                $new_link = "rel=\"nofollow\" target=\"_blank\" href=\"" . get_bloginfo('wpurl') . "/go?url=" . $link . "\"";
                $content = str_replace("href=\"$link\"", $new_link, $content);
            }
        }
    }

    return $content;
}

注意修改相应部分。然后在网站根目录下新建个 go 的文件夹,在其中写个 index.php 的文件,内容如下(请保存为 UTF-8):

<?php
$url = $_GET['url'];
//$url = base64_decode($url); // 关闭地址转换
?>
<html>
<head>
<meta charset=utf-8 />
<meta name="robots" content="nofollow">
<title>正在为您跳转……</title>
<style>
    body {
        font-family: Arial, sans-serif;
        text-align: center;
        padding: 20px;
    }
    .countdown {
        font-size: 24px;
        color: #333;
    }
    .button {
        display: inline-block;
        padding: 10px 16px;
        color: #fff;
        font-size: 14px;
        line-height: 1;
        background-color: #0077d9;
        border-radius: 3px;
        text-decoration: none;
    }
    .button:hover {
        background-color: #0070cd;
    }
    .button:active {
        background-color: #0077d9;
    }
</style>
</head>
<body>
    <div class="logo">
        <a href="https://www.iamlm.com">
            <img src="https://www.iamlm.com/favicon.svg" width="48" height="48" alt="老麦笔记">
        </a>
    </div>
    <div class="wrapper">
        <div class="content">
            <h1>即将离开老麦笔记</h1>
            <p class="countdown"><span id="countdown">5</span>秒后进入<?php echo $url; ?></p>
            <p class="info">您即将进行页面跳转,请注意您的帐号和财产安全。</p>
            <p class="link"></p>
        </div>
        <div class="actions">
            <a class="button" href="<?php echo $url; ?>">直接访问</a>
        </div>
    </div>
    <script>
        // 设置初始倒计时时间
        let timeLeft = 25;

        // 每秒更新倒计时时间并跳转
        let countdown = setInterval(function() {
            timeLeft--;
            document.getElementById('countdown').textContent = timeLeft;
            if (timeLeft <= 0) {
                clearInterval(countdown);
                window.location.href = '<?php echo $url; ?>'; // 替换为实际跳转的网址
            }
        }, 1000);
    </script>
</body>
</html>

至此基本上大功告成了,亲们可以点击这里(weibo.com/finle) 试试效果怎么样…你可以对其中的倒计时、网页样式表、网页内容等进行修改。更进一步,还可以对原始链接进行 base64 加密,尽管代码中已经用base64将源链接加密并且加上了 nofollow,但恐怕蜘蛛还是能爬行,所以干脆一不做二不休,在 Robots 禁止所有蜘蛛爬行 /go?url 目录吧!

580 380 Kevin's
「WordPress 添加文章外链跳转中间页面」有 10 条评论
  • 东莞地坪博客
    12/03/2016 at 16:42 回复

    找代码搞不定,还是直接用插件方便。

    • 牧羊人
      12/05/2016 at 13:48 回复

      太多插件了也不好,卡。。

  • fooleap
    11/28/2016 at 08:10 回复

    虽然看起来很棒,但个人感觉,像我自己流量本并不多的博客站,没必要搞那么花哨,还是简简单单,外链就是外链。再者,流量够高,可能也不在意。

    • 牧羊人
      11/28/2016 at 19:21 回复

      说的在理。所以我取消了,哈哈哈

      btw:这是我以前一个网站上用 的,只是分享给有需要的人

  • 拾叁
    11/21/2016 at 06:45 回复

    请问 如果不是WordPress该如何实现呢?

    • 牧羊人
      11/21/2016 at 21:58 回复

      额,这个还真不好说了。。。。毕竟,我wp 都整不利索啊。。

  • 老杨
    11/13/2016 at 20:37 回复

    可是,你这并没有效果,哈哈。

    • 牧羊人
      11/19/2016 at 14:29 回复

      我取消了,因为好像没啥用,ahhh

  • 神父
    11/03/2016 at 14:54 回复

    我记得你以前写过啊,有什么改进吗

    • 牧羊人
      11/04/2016 at 09:33 回复

      没有,因为 youth daily 很可能就关闭了,搬过来 ::sad::

发表评论

请输入关键词…