Home / Admin / Overwriting Entries From Users Who Have Already Submitted a Form
Duplicate Snippet

Embed Snippet on Your Site

Overwriting Entries From Users Who Have Already Submitted a Form

This snippet will overwrite old entries from a user each time they submit a new one for a specific form. The user needs to be logged in to your site for this snippet to work.

<10
Code Preview
php
<?php
/*
 * Remove all user entries before saving the entry - this is for all forms.
 *
 * @link https://wpforms.com/developers/how-to-overwrite-entries-from-users-who-have-already-submitted-a-form/
 */
 
function remove_all_before_entry_save( $fields, $entry, $form_id ) {
 
        //get the current user's user ID
    $user_id = get_current_user_id();
 
        //check if the user is logged in, if not logged in skip this code snippet all together
    if ( empty( $user_id ) ) {
        return;
    }
 
        //perform a query for any entries submitted on this form by this user ID
    $entries = wpforms()->entry->get_entries(
        [
            'form_id' => $form_id,
            'user_id' => $user_id,
            'number'  => -1,
            'select'  => 'entry_ids',
            'cap'     => false,
        ]
    );
 
        //for any previous entries this user has submitted on this form, remove them and replace them with this entry only 
    foreach ( $entries as $_entry ) {
        wpforms()->entry->delete( $_entry->entry_id, [ 'cap' => false ] );
    }
}
add_action( 'wpforms_process_entry_save', 'remove_all_before_entry_save', 9, 3 );

Comments

Add a Comment