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