Search code examples
wordpressgravity-forms-plugin

Retain the form in a gravity form confirmation


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.

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)

Thanks in advance


Solution

  • This is how I did it.

    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;
    
    }
    

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

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

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

    $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.

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

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

    This is also tested on a page where it contains only 1 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.

    You can get the current gform's id via $form['id']

    Hope this helps anyone.