保留重力形式确认形式 [英] Retain the form in a gravity form confirmation

查看:18
本文介绍了保留重力形式确认形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在重力表单确认期间(在表单成功填写并用户提交表单之后),我不仅希望显示确认文本,还希望在确认文本下方显示确认文本和表单本身.

During gravity form confirmation (after form is successfully filled up and user submits the form) instead of showing only the confirmation text, I would also like to display the confirmation text and the form itself also, below the confirmation text.

我不能在 gform 的确认设置中使用重定向到页面 url 或页面选项,因为用户通过短代码在各种页面上使用表单(其中一些甚至通过 do_shortcode 硬编码),他正在计划添加更多.

I can't use the redirect to page url or page options on the confirmation settings of a gform, coz the user is using the form on various pages via shortcode (some of em are even hard coded via do_shortcode), and he is planning to add more.

这个想法是,在用户填写并提交表单后(无论表单驻留在哪个页面),表单数据将被提交,页面重新加载,显示确认消息并显示实际表单(当然所有数据都消失了,新鲜状态)

The idea is, after the user fills up and submits the form ( no matter what page the form is residing ), the form data would get submitted, page reloaded, a confirmation message is displayed and the actual form is displayed too (of course all data gone, fresh state)

提前致谢

推荐答案

我就是这样做的.

add_filter( 'gform_pre_submission_filter' , "foo" , 10 , 1 );

function foo ( $form ) {

    global $post;

    // Get current page url
    $current_page_url = get_post_permalink( $post->ID );

    if ( array_key_exists( 'confirmations' , $form ) ) {

        foreach ( $form[ 'confirmations' ] as $key => $confirmation ) {

            $form[ 'confirmations' ][ $key ][ 'type' ] = 'redirect';
            $form[ 'confirmations' ][ $key ][ 'message' ] = '';
            $form[ 'confirmations' ][ $key ][ 'url' ] = $current_page_url;
            $form[ 'confirmations' ][ $key ][ 'queryString' ] = 'message=Form Data Saved';

        }

    }

    if ( array_key_exists( 'confirmation' , $form ) ) {

        $form[ 'confirmation' ][ 'type' ] = 'redirect';
        $form[ 'confirmation' ][ 'message' ] = '';
        $form[ 'confirmation' ][ 'url' ] = $current_page_url;
        $form[ 'confirmation' ][ 'queryString' ] = 'message=Form Data Saved';

    }

    return $form;

}

钩入 gform_pre_submission_filter 过滤器,该钩子的回调将接收 $form 变量.

Hook into the gform_pre_submission_filter filter, the callback for that hook will receive the $form variable.

$form 是一个数组,其中包含有关如何将其确认为子数组的详细信息.

$form is an array which contains details for how it handles it confirmation as a sub array.

它有 2 个,$form['confirmations'],包含为此特定 gform 注册的所有确认.

It has 2, $form['confirmations'], contains all the confirmations registered for this particular gform.

$form['confirmation'],包含要使用的默认确认"的数据.更改两者,在我的情况下,我将其更改为 'redirect' 类型,然后提供要重定向的 url.

$form['confirmation'], contains data of the "Default Confirmation" to use. Change both, in my case I change it to 'redirect' type then supplied the url to redirect.

我也加了一个查询字符串数据,这里加空格没问题,gform会处理转义.

I added a query string data too, its ok to add spaces here, gform will take care of the escaping.

这是在一个只有 1 个确认的 gform 上测试的,这是默认确认.

This is tested on a gform with only 1 confirmation, which is the Default Confirmation.

这也在仅包含 1 个 gform 的页面上进行了测试.

This is also tested on a page where it contains only 1 gform.

如果您有一个包含多个 gform 的页面,这很简单,只需将相关 gform 的 id 添加到 queryString 中即可.这样就更容易提取查询字符串并确定确认消息属于哪个 gform.

If you have a page with multiple gforms, its easy, just add the id of the gform in question to the queryString. Then its much easier to extract the query string and identify which gform a confirmation message pertains to.

您可以通过 $form['id']

希望对大家有所帮助.

这篇关于保留重力形式确认形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆