返回上一级

WordPress 简单代码实现 外链跳转安全提示

通过代码方式实现知乎那种外链跳转提示

可以自行定制提示页面

目前不清楚对搜索引擎是正收益还是负收益

核心代码

使用get传参进行传值,如以下这样

1www.krjojo.com/link?url=base64网址

如果需要把网址直接写到url里面,可以自行小调整实现

1www.krjojo.com/link/base64网址/

改写文中所有url地址,进行base64编码

注释掉的那行为url传值,需要自己另外实现方法截取地址请求地址

 1<?php
 2
 3// 改写文中所有url地址,进行base64编码
 4add_filter('the_content', function ($content) {
 5    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/', $content, $matches);
 6    if ($matches) {
 7        foreach ($matches[2] as $val) {
 8            if (strpos($val, '://') !== false && strpos($val, home_url()) === false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $val)) {
 9                $content = str_replace("href=\"$val\"", "href=\"" . home_url() . "/link?url=" . base64_encode($val) . "\"  target=_blank ", $content);
10                // $content = str_replace("href=\"$val\"", "href=\"" . home_url() . "/link/" . base64_encode($val) . "\"  target=_blank ", $content);
11            }
12        }
13    }
14    return $content;
15});

添加提示页面模板

创建 link-tempate.php 模板文件,用于自定义返回页面内容

PHP负责解码base64,获得url地址。

 1<?php
 2// $link = base64_decode(substr($_SERVER['REQUEST_URI'], strlen('/link/')));
 3$link = base64_decode($_GET['url'] ?? "");
 4?>
 5<!doctype html>
 6<html lang="zh">
 7
 8<head>
 9    <meta charset="UTF-8">
10    <meta name="viewport"
11        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
12    <meta http-equiv="X-UA-Compatible" content="ie=edge">
13    <meta name='description' content='您即将离开 手里有只毛毛虫,请注意您的账号财产安全。'>
14    <title>手里有只毛毛虫</title>
15    <style>
16        html {
17            background: #f4f5f5;
18        }
19
20        #box {
21            margin: auto;
22            background: #fff;
23            padding: 10px 30px;
24            margin-top: 10%;
25            max-width: 500px;
26            box-sizing: border-box;
27            border: 1px solid #e5e6eb;
28            border-radius: 2px;
29        }
30
31        .note {
32            font-size: 16px;
33            line-height: 20px;
34        }
35
36        .link {
37            padding: 16px 0 24px;
38            border-bottom: 1px solid #e5e6eb;
39            position: relative;
40            color: gray;
41            font-family: "PingFang SC";
42            font-size: 14px;
43            word-break: break-all;
44
45        }
46
47        .btn-plane {
48            text-align: right;
49        }
50
51        button {
52            margin-top: 20px;
53            color: #fff;
54            border-radius: 3px;
55            border: none;
56            background: #007fff;
57            height: 32px;
58            font-size: 14px;
59            padding: 0 14px;
60            cursor: pointer;
61            outline: 0;
62        }
63
64        button a {
65            color: #fff;
66            text-decoration: none
67        }
68    </style>
69</head>
70
71<body>
72    <div id="box">
73        <p class="note">您即将离开
74            <a href="/" style="text-decoration:none;">
75                <?php echo get_bloginfo('name') ?>
76            </a>,请注意您的账号财产安全
77        </p>
78        <p class="link">
79            <?php echo $link ?: "发生错误" ?>
80        </p>
81        <p class="btn-plane">
82            <a href="<?php echo $link ?: "/" ?>" rel="nofollow">
83                <button>
84                    <?php echo $link ? "继续访问" : "返回主页" ?>
85                </button>
86            </a>
87        </p>
88    </div>
89</body>
90
91</html>

绑定提示页面模板

绑定外链跳转安全提示页面的模板

__DIR__ . '/link-tempate.php'; 表示同级目录下的 link-tempate.php 文件

 1<?php
 2
 3// 页面模板添加一个 外链跳转安全提示页面 的选项
 4add_filter('theme_page_templates', function ($page_templates) {
 5    $page_templates["link.php"] = '外链跳转安全提示页面';
 6    return $page_templates;
 7});
 8
 9// 打开页面时,如果是我们的模板,则返回模板文件的路径
10add_filter('template_include',  function ($template) {
11    $template_name = get_post_meta(get_the_ID(), '_wp_page_template', true);
12
13    if ($template_name == 'link.php') {
14        return __DIR__ . '/link-tempate.php';
15    }
16    return $template;
17});

添加页面

以上代码添加后,在后台创建页面的右侧,可以看到选择模板,

把页面模板改为刚刚新建的那个 外链跳转安全提示页面 ,网址的固定链接改为 link

点击发布,完成