-
-
Save davidwolfpaw/0fa37230c9dbb197ed4a8bbc1c7e9547 to your computer and use it in GitHub Desktop.
<?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 | |
} |
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' );
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) );
});
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/
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 :(
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
I made a little tweak and it is working for me with the latest version. Here's my code: