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

WordPress functions.php重定向需排除/template/目录问题求助

解决方案

首先修正原代码里的一处拼写错误:$_SERVER['HTTPS_HOST']应为$_SERVER['HTTP_HOST']。

要排除/template/路径的请求,我们可以直接检查当前请求的URI是否包含该路径,结合后台排除逻辑,修改后的代码如下:

add_action( 'template_redirect', 'wa_redirect_site', 5 );
function wa_redirect_site(){
    // 排除WordPress后台请求
    if ( is_admin() ) {
        return;
    }
    
    // 获取当前请求的URI
    $request_uri = $_SERVER['REQUEST_URI'];
    
    // 排除包含/template/的路径(适配Bricks模板编辑场景)
    if ( strpos( $request_uri, '/template/' ) !== false ) {
        return;
    }
    
    // 执行临时重定向到首页
    wp_redirect( 'https://wa-leicester.org.uk/', 302 );
    exit;
}

代码说明

  • is_admin():直接判断当前是否为后台请求,比原代码的逻辑更可靠。
  • strpos( $request_uri, '/template/' ) !== false:检查请求URI中是否包含/template/字符串,只要匹配就跳过重定向,完美适配Bricks Builder的模板编辑URL(如.../template/115/?bricks=run)。
  • 移除了原代码中冗余的url_to_postid和is_page判断,简化逻辑的同时避免不必要的错误。

如果需要额外排除特定页面,可在判断条件中添加is_page(),示例:

if ( is_admin() || strpos( $request_uri, '/template/' ) !== false || is_page( 123 ) ) {
    return;
}

内容的提问来源于stack exchange,提问作者Shaun Taylor

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 04:15:25