One WPForms Submission per Logged-In User
Lets each logged-in member submit the WPForms forms you choose only once, for surveys, votes or applications, and replaces the form with a message afterwards.
Opens WPIDE on
Code
$wpide_wpf_once_forms = array(); // Form IDs, e.g. array( 1 ).
$wpide_wpf_once_message = 'You have already submitted this form. Thank you!';
add_filter( 'wpforms_frontend_load', function ( $load, $form_data ) use ( $wpide_wpf_once_forms, $wpide_wpf_once_message ) {
$id = isset( $form_data['id'] ) ? (int) $form_data['id'] : 0;
if ( $load && is_user_logged_in() && in_array( $id, array_map( 'intval', $wpide_wpf_once_forms ), true ) && get_user_meta( get_current_user_id(), '_wpide_wpf_done_' . $id, true ) ) {
echo '<p class="wpide-wpf-once" role="status">' . esc_html( $wpide_wpf_once_message ) . '</p>';
return false;
}
return $load;
}, 10, 2 );
add_action( 'wpforms_process_before', function ( $entry, $form_data ) use ( $wpide_wpf_once_forms, $wpide_wpf_once_message ) {
$id = (int) $form_data['id'];
if ( is_user_logged_in() && in_array( $id, array_map( 'intval', $wpide_wpf_once_forms ), true ) && get_user_meta( get_current_user_id(), '_wpide_wpf_done_' . $id, true ) ) {
$process = method_exists( wpforms(), 'obj' ) ? wpforms()->obj( 'process' ) : wpforms()->process;
$process->errors[ $id ]['header'] = $wpide_wpf_once_message;
}
}, 10, 2 );
add_action( 'wpforms_process_complete', function ( $fields, $entry, $form_data ) use ( $wpide_wpf_once_forms ) {
$id = (int) $form_data['id'];
if ( is_user_logged_in() && in_array( $id, array_map( 'intval', $wpide_wpf_once_forms ), true ) ) {
update_user_meta( get_current_user_id(), '_wpide_wpf_done_' . $id, time() );
}
}, 10, 3 );
Notes
List the form IDs at the top; submissions are remembered in user meta, so it works in WPForms Lite too, and logged-out visitors are not limited.
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.