Forked from davidwolfpaw/gravity-forms-confirmation-remain.php
Created
April 1, 2024 06:26
-
-
Save infocities/a06fa41297c7aed951f9b8566422f738 to your computer and use it in GitHub Desktop.
Keep Gravity Forms Displayed After Submission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment