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

如何基于ACF动态设置Contact Form7表单提交后的重定向

实现Contact Form7提交后跳转到ACF存储的房源手册URL

完全可以基于ACF中的数据设置动态重定向,以下是具体落地步骤:

1. 给Contact Form7表单添加隐藏字段

在弹窗对应的CF7表单编辑器中,新增一个隐藏字段,用于存储当前房源的手册URL:

[hidden property_manual_url]

2. 在房源页面动态填充隐藏字段值

在房源详情模板(如single-property.php)中,通过PHP将ACF字段的URL传递给CF7表单的隐藏字段。假设你的ACF字段名为property_manual,代码示例如下:

<?php
// 获取当前房源的手册URL
$manual_url = get_field('property_manual');
// 输出CF7表单并动态赋值隐藏字段
echo do_shortcode('[contact-form-7 id="123" title="下载手册表单" property_manual_url="' . esc_url($manual_url) . '"]');
?>

注意:替换id="123"为你实际的CF7表单ID,property_manual替换为你的ACF字段名称

3. 通过CF7钩子实现提交后重定向

在主题的functions.php文件中添加以下代码,利用wpcf7_mail_sent钩子在表单提交成功后,跳转到隐藏字段存储的手册URL:

add_action('wpcf7_mail_sent', 'redirect_to_property_manual');
function redirect_to_property_manual($contact_form) {
    $submission = WPCF7_Submission::get_instance();
    if (!$submission) return;

    $posted_data = $submission->get_posted_data();
    $manual_url = isset($posted_data['property_manual_url']) ? esc_url($posted_data['property_manual_url']) : '';

    if (!empty($manual_url)) {
        wp_redirect($manual_url);
        exit;
    }
}

额外优化提示

  • 确保ACF字段property_manual返回完整的URL(包含http/https前缀)
  • 若弹窗通过JS动态加载(如Elementor弹窗),可改用JS给隐藏字段赋值:
document.addEventListener('DOMContentLoaded', function() {
    // 从页面全局变量获取手册URL(模板中用PHP输出)
    const manualUrl = '<?php echo esc_js(get_field("property_manual")); ?>';
    // 给CF7隐藏字段赋值
    const hiddenField = document.querySelector('input[name="property_manual_url"]');
    if (hiddenField) hiddenField.value = manualUrl;
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 03:43:09