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

ACF表单提交未按选择跳转Stripe支付链接问题排查求助

问题根因及修复方案

核心错误点

  • 错误覆盖传入的$post_id参数
    你写的函数已经通过acf/save_post钩子拿到了正确的新提交文章ID作为入参,但代码第一行就用$post_id = get_the_ID();强行覆盖了这个值。首次提交新文章时,全局文章对象还未初始化,get_the_ID()无法返回正确的新建文章ID,导致后续所有字段读取、文章类型判断逻辑全部失效;而编辑已发布文章时,页面加载时已经初始化了全局文章对象,get_the_ID()可以拿到正确ID,所以跳转正常。
  • 逻辑运算符被HTML转义
    代码里的条件判断用了&&,这是HTML转义后的字符,PHP无法正确识别为逻辑与运算符,会导致条件判断异常。
  • 缺少ACF字段读取容错处理
    如果repeater字段未填写,原代码不会执行任何跳转逻辑,没有兜底处理。

修复后的代码

add_filter('acf/save_post', 'stripe_payment_redirect', 20);
function stripe_payment_redirect($post_id) {
    // 直接使用钩子传入的$post_id,不要用get_the_ID()覆盖
    if (is_admin() || get_post_type($post_id) != 'post_type_listings') {
        return;
    }
       
    if (have_rows('listing_payment_options', $post_id)) {
        while (have_rows('listing_payment_options', $post_id)) {
            the_row();
            $package_choice = get_sub_field('listing_package_options'); 
            $featured       = get_sub_field('listing_featured_listing');
            
            // 用原生PHP的&&逻辑运算符,不要用转义后的&&
            if ($package_choice == '6 Monthly' && $featured == 1) {
                wp_redirect('https://buy.stripe.com/test_28o4jW3dGgpTaQwdQV'); 
                exit;
            } elseif ($package_choice == 'Yearly' && $featured == 1) {
                wp_redirect('https://buy.stripe.com/test_28o3fS15y6Pj7EkbIL'); 
                exit;
            } elseif ($package_choice == 'Yearly') {
                wp_redirect('https://buy.stripe.com/test_dR66s415y1uZ5wceUW'); 
                exit;
            } else {
                wp_redirect('https://buy.stripe.com/test_14kbMoeWo0qVgaQ5ko'); 
                exit;
            }
        }
    }  
    // 补充repeater字段为空时的兜底跳转,可根据需求修改目标链接
    wp_redirect(home_url());
    exit;
}

额外注意事项

  • 确保ACF前端表单没有提前输出任何内容(包括空格、PHP警告等),否则wp_redirect会因为header已发送报错。
  • 优先级设置为20是正确的,ACF默认在优先级10时保存字段,20可以保证读取到最新提交的字段值。

内容的提问来源于stack exchange,提问作者Huw Rowlands

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 00:27:03