One Gravity Forms Entry per Logged-In User
Lets each logged-in member submit a Gravity Forms form only once, for surveys, applications or votes, and shows a friendly message instead of the form afterwards.
Opens WPIDE on
Code
$wpide_gf_once_forms = array(); // Form IDs, e.g. array( 1 ).
$wpide_gf_once_message = 'You have already submitted this form. Thank you!';
$wpide_gf_user_has_entry = function ( $form_id ) {
$user = get_current_user_id();
return $user && GFAPI::count_entries( (int) $form_id, array( 'status' => 'active', 'field_filters' => array( array( 'key' => 'created_by', 'value' => $user ) ) ) ) > 0;
};
add_filter( 'gform_get_form_filter', function ( $html, $form ) use ( $wpide_gf_once_forms, $wpide_gf_once_message, $wpide_gf_user_has_entry ) {
if ( in_array( (int) $form['id'], array_map( 'intval', $wpide_gf_once_forms ), true ) && $wpide_gf_user_has_entry( $form['id'] ) ) {
return '<p class="wpide-gf-once" role="status">' . esc_html( $wpide_gf_once_message ) . '</p>';
}
return $html;
}, 10, 2 );
add_filter( 'gform_validation', function ( $validation ) use ( $wpide_gf_once_forms, $wpide_gf_once_message, $wpide_gf_user_has_entry ) {
$form = $validation['form'];
if ( in_array( (int) $form['id'], array_map( 'intval', $wpide_gf_once_forms ), true ) && $wpide_gf_user_has_entry( $form['id'] ) ) {
$validation['is_valid'] = false;
foreach ( $form['fields'] as $field ) {
if ( ! in_array( $field->type, array( 'hidden', 'html', 'section', 'page' ), true ) ) {
$field->failed_validation = true;
$field->validation_message = $wpide_gf_once_message;
break;
}
}
$validation['form'] = $form;
}
return $validation;
} );
Notes
List the form IDs at the top and turn on "Require user to be logged in" in those forms' settings.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.