Skip to content

Instantly share code, notes, and snippets.

@davidwolfpaw
Last active April 1, 2024 06:26
Show Gist options
  • Save davidwolfpaw/0fa37230c9dbb197ed4a8bbc1c7e9547 to your computer and use it in GitHub Desktop.
Save davidwolfpaw/0fa37230c9dbb197ed4a8bbc1c7e9547 to your computer and use it in GitHub Desktop.
Keep Gravity Forms Displayed After Submission
<?php
// Allow the Gravity form to stay on the page when confirmation displays.
add_filter( 'gform_pre_submission_filter', 'dw_show_confirmation_and_form' );
function dw_show_confirmation_and_form( $form ) {
// Inserts a shortcode for the form without title or description
$shortcode = '[gravityform id="' . $form['id'] . '" title="false" description="false"]';
// Ensures that new lines are not added to HTML Markup
ob_start();
echo do_shortcode($shortcode);
$html = str_replace(array("\r","\n"),'',trim(ob_get_clean()));
// Inserts the form before the confirmation message
if ( array_key_exists( 'confirmations', $form ) ) {
foreach ( $form['confirmations'] as $key => $confirmation ) {
$form['confirmations'][ $key ]['message'] = $html . '<div class="confirmation-message">' . $form['confirmations'][ $key ]['message'] . '</div>';
}
}
return $form;
}
// Insert Javascript into the site footer to clear Gravity Forms inputs after submission
add_action( 'wp_footer', 'dw_gf_footer_scripts' );
function dw_gf_footer_scripts() {
?>
<script>
// Get all form inputs into arrays
const inputs = document.querySelectorAll('.gform-body input');
const textareas = document.querySelectorAll('.gform-body textarea');
const inputsArray = Array.from(inputs);
const textareasArray = Array.from(textareas);
// Run clearValues on each input and textarea
inputsArray.forEach(clearValues);
textareasArray.forEach(clearValues);
// Clear the values of inputs
function clearValues( elem ) {
// Do not clear hidden values
if(elem.type !== 'hidden') {
elem.value = '';
}
}
</script>
<?php
}
@jandagroup
Copy link

Unfortunately, this no longer works... I'm not sure if it's because of a new WP version or a new GF version... But it breaks.

The confirmation message displays, but then instead of the form getting injected back into the confirm message, I get an "Oops! We could not locate your form."

I've reached out to GF support and they said they've "never" supported this behavior and that it should never have worked in the first place... Which, is ridiculous because I've used this approach for years :) Anyways, any thoughts as to what happened?

Thanks!

@ShariqKhan2012
Copy link

I made a little tweak and it is working for me with the latest version. Here's my code:

function dw_show_confirmation_and_form( $form ) {

	$shortcode = '[gravityform id="' . $form['id'] . '" title="false" description="false"]';

	ob_start();
	echo do_shortcode($shortcode);
	$html = ob_get_clean();

	if ( array_key_exists( 'confirmations', $form ) ) {
		foreach ( $form['confirmations'] as $key => $confirmation ) {
			$form['confirmations'][ $key ]['message'] = $html . '<div class="confirmation-message">' . $form['confirmations'][ $key ]['message'] . '</div>';
		}
	}

	return $form;
}
add_filter( 'gform_pre_submission_filter', 'dw_show_confirmation_and_form' );

@mark-alexandre
Copy link

mark-alexandre commented Oct 19, 2021

My little contribution to this : ob_get_clean() will potentially add new lines, which will be visible with
HTML markup. In order to get rid of this, use this instead ->

function dw_show_confirmation_and_form( $form ) {

    $shortcode = '[gravityform id="' . $form['id'] . '" title="false" description="false"]';

    ob_start();
    echo do_shortcode($shortcode);
    $html = str_replace(array("\r","\n"),'',trim(ob_get_clean()));

    if ( array_key_exists( 'confirmations', $form ) ) {
        foreach ( $form['confirmations'] as $key => $confirmation ) {
            $form['confirmations'][ $key ]['message'] = $html . '<div class="confirmation-message">' . $form['confirmations'][ $key ]['message'] . '</div>';
        }
    }

    return $form;
}
add_filter( 'gform_pre_submission_filter', 'dw_show_confirmation_and_form' );

@joelmcdwebworks
Copy link

joelmcdwebworks commented Nov 3, 2021

Thanks @mark-alexandre! This worked for me. The only thing missing is clearing the form fields after the confirmation loads. I used jQuery to do this. I only targeted visible fields to avoid overwriting any hidden form values. Code below:

function clearValues( elem ) {

    if($(elem).is(":visible")) {

        $(elem).val("");

    }
}

$(".gform-body input").each( function() {

    clearValues( $(this) );

});

$(".gform-body textarea").each( function() {

    clearValues( $(this) );

});

@davidwolfpaw
Copy link
Author

Thank you all for your suggestions and help rewriting the code! I've updated the gist, as well as the blog post that refers to it: https://davidwolfpaw.com/how-to-keep-gravity-forms-displayed-after-submission/

@adxmeliora
Copy link

adxmeliora commented Mar 14, 2024

Been trying to solve this problem for ages with so many solutions online that didn't work at all. Finally came across your code... works absolutely perfectly. Can't thank you enough :)

Edit: Damn, spoke too soon. Sadly it doesn't seem to be compatible with Populate Anything :(

@davidwolfpaw
Copy link
Author

davidwolfpaw commented Mar 15, 2024

Edit: Damn, spoke too soon. Sadly it doesn't seem to be compatible with Populate Anything :(

I'm not familiar with how Populate Anything works. I could take a look if you have a link to a page with a form that you want to have this work on. @adxmeliora

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment