You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress联盟站外部链接2秒停留跳转页开发需求

WordPress联盟站外部链接2秒自动跳转实现方案

1. 自定义跳转页模板

在主题目录新建redirect-template.php文件,写入以下代码:

<?php
/*
Template Name: 外部链接跳转页
*/
get_header(); ?>

<div class="redirect-wrap">
    <h2>正在跳转,请稍候...</h2>
    <p>如果未自动跳转,请点击 <a id="direct-link" href="">手动跳转</a></p>
</div>

<script>
// 提取URL中的目标链接参数
const target = new URLSearchParams(window.location.search).get('url');
const directLink = document.getElementById('direct-link');

if (target) {
    directLink.href = target;
    // 2秒后在新标签打开目标页
    setTimeout(() => {
        window.open(target, '_blank');
    }, 2000);
} else {
    document.querySelector('.redirect-wrap p').textContent = '无效跳转请求,请返回原页面';
}
</script>

<?php get_footer(); ?>

2. 创建跳转页面

  • 后台新建页面,标题设为「跳转中」,页面模板选择「外部链接跳转页」,发布后记下页面URL(比如https://你的站点.com/going-to/)

3. 配置Redirection插件规则

  • 打开Redirection插件,添加新规则:
    • 来源URL:用正则匹配所有外部链接,示例:^https?://(?!your-domain\.com).*(替换your-domain.com为你的实际域名)
    • 目标URL:填写跳转页URL并拼接参数:https://你的站点.com/going-to/?url=$0
    • 匹配类型:选择「正则表达式」
    • 动作类型:「重定向到URL」,状态码选302

4. 可选:美化跳转页样式

在主题style.css中添加样式:

.redirect-wrap {
    text-align: center;
    padding: 60px 20px;
    max-width: 600px;
    margin: 50px auto;
}
.redirect-wrap h2 {
    color: #2d3748;
    margin-bottom: 1.5rem;
}
.redirect-wrap a {
    color: #2b6cb0;
    font-weight: 500;
}

关键注意点

  • 浏览器可能拦截非用户触发的弹窗,所以必须保留手动跳转链接作为备用方案
  • 正则表达式要准确排除本站链接,避免内部跳转被错误拦截
  • 测试前清除浏览器缓存,确保Redirection规则正常生效

内容的提问来源于stack exchange,提问作者ahmet berat yeniay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 22:15:28