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

如何将Elementor表单评估结果传递至重定向页面并渲染模板

解决方案:将Elementor表单评估结果传递到重定向页面

方法一:使用PHP会话(Session)存储结果

适合需要传递较多内容或不想暴露参数在URL中的场景。

步骤1:初始化会话

在functions.php中添加会话初始化代码,确保表单提交后能存储会话数据:

add_action('init', function() {
    if (!session_id()) {
        session_start();
    }
});

步骤2:修改表单提交钩子代码

替换你原有的elementor_pro/forms/new_record钩子代码,将评估结果存入会话:

add_action( 'elementor_pro/forms/new_record', function ( $record, $handler ) {
    $form_name = $record->get_form_settings( 'form_name' );
    // 替换为你的表单实际名称
    if ('你的表单名称' !== $form_name) {
        return;
    }

    $raw_fields = $record->get( 'fields' );
    $fields = [];
    foreach ( $raw_fields as $id => $field ) {
        $fields[ $id ] = $field['value'];
    }
    
    $variableName = '';
    if ($fields['a'] == 'answer a') {
        $variableName .= '[elementor-template id="1"]';
    }
    if ($fields['b'] == 'answer b') {
        $variableName .= '[elementor-template id="2"]';
    }   

    // 将结果存入会话
    $_SESSION['form_template_result'] = $variableName;

    // 若表单未在后台设置重定向,可手动指定重定向页面(替换为你的页面ID)
    // wp_redirect( get_permalink( 123 ) );
    // exit;
}, 10, 2 );

步骤3:创建显示结果的短代码

在functions.php中添加短代码,用于在重定向页面读取并解析模板短代码:

add_shortcode('display_form_template_result', function() {
    if (!isset($_SESSION['form_template_result'])) {
        return '暂无匹配结果';
    }
    // 解析Elementor模板短代码并输出
    $content = do_shortcode($_SESSION['form_template_result']);
    // 清空会话,避免页面刷新重复显示
    unset($_SESSION['form_template_result']);
    return $content;
});

步骤4:在重定向页面添加短代码

打开重定向页面的Elementor编辑器,添加「短代码」小部件,输入[display_form_template_result]即可。


方法二:通过URL参数传递模板ID

更轻量,适合仅传递模板ID的场景。

步骤1:修改表单提交钩子代码

将评估后的模板ID拼接为重定向URL的参数:

add_action( 'elementor_pro/forms/new_record', function ( $record, $handler ) {
    $form_name = $record->get_form_settings( 'form_name' );
    if ('你的表单名称' !== $form_name) {
        return;
    }

    $raw_fields = $record->get( 'fields' );
    $fields = [];
    foreach ( $raw_fields as $id => $field ) {
        $fields[ $id ] = $field['value'];
    }
    
    $template_ids = [];
    if ($fields['a'] == 'answer a') {
        $template_ids[] = 1;
    }
    if ($fields['b'] == 'answer b') {
        $template_ids[] = 2;
    }   

    // 拼接模板ID为URL参数
    $template_param = implode(',', $template_ids);
    // 替换为你的重定向页面ID
    $redirect_url = get_permalink( 123 );
    // 添加参数到URL
    $redirect_url = add_query_arg('templates', $template_param, $redirect_url);

    // 执行重定向
    wp_redirect($redirect_url);
    exit;
}, 10, 2 );

步骤2:创建显示模板的短代码

在functions.php中添加短代码,读取URL参数并渲染对应模板:

add_shortcode('display_form_templates', function() {
    if (!isset($_GET['templates'])) {
        return '暂无匹配结果';
    }
    $template_ids = explode(',', $_GET['templates']);
    $content = '';
    foreach ($template_ids as $id) {
        $content .= do_shortcode("[elementor-template id='{$id}']");
    }
    return $content;
});

步骤3:在重定向页面添加短代码

同样在重定向页面的Elementor编辑器中添加「短代码」小部件,输入[display_form_templates]即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 08:01:00