Last active
December 3, 2021 14:58
-
-
Save spivurno/53e94532ce38efbd49c05fb16122436b to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Prevent Duplicate Submissions from Double Clicks
This file contains 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 | |
/** | |
* WARNING! THIS SNIPPET MAY BE OUTDATED. | |
* The latest version of this snippet can be found in the Gravity Wiz Snippet Library: | |
* https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-prevent-duplicate-submissions.php | |
*/ | |
/** | |
* Gravity Wiz // Gravity Forms // Prevent Duplicate Submissions from Double Clicks | |
* http://gravitywiz.com/ | |
*/ | |
add_filter( 'gform_pre_render', 'gw_disable_submit' ); | |
function gw_disable_submit( $form ) { | |
if( defined( 'DOING_AJAX' ) && DOING_AJAX ) { | |
return $form; | |
} | |
?> | |
<script type="text/javascript"> | |
jQuery( document ).ready( function( $ ) { | |
var formId = '<?php echo $form['id']; ?>'; | |
$( '#gform_submit_button_' + formId ).on( 'click', function( event ) { | |
if( hasPendingFileUploads( formId ) ) { | |
return; | |
} | |
var $submitCopy = $( this ).clone(); | |
$submitCopy | |
.attr( 'id', '' ) | |
.prop( 'disabled', true ) | |
.attr( 'value', 'Processing...' ) | |
.insertBefore( $( this ) ); | |
$( this ).css( { visibility: 'hidden', position: 'absolute', transition: 'all 0s ease 0s' } ); | |
} ); | |
function hasPendingFileUploads() { | |
if( ! window[ 'gfMultiFileUploader' ] ) { | |
return false; | |
} | |
var pendingUploads = false; | |
$.each( gfMultiFileUploader.uploaders, function( i, uploader ) { | |
if( uploader.total.queued > 0 ) { | |
pendingUploads = true; | |
return false; | |
} | |
} ); | |
return pendingUploads; | |
} | |
} ); | |
</script> | |
<?php | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Glad to help, @sperez91!