Home / Admin / Increment a Count on Each Form Submission
Duplicate Snippet

Embed Snippet on Your Site

Increment a Count on Each Form Submission

This snippet counts the total entries for a specific form and updates a hidden field with an incremented, zero-padded value on each submission. The count starts at 1 for new forms or continues from the existing entry count.

<10
Code Preview
php
<?php
/**
 * Increment total entry number on each submission
 *
 * @link https://wpforms.com/developers/how-to-increment-a-count-on-each-form-submission
 */
 
function wpf_dev_update_total_field( $fields, $entry, $form_data ) {
     
	$my_form_id = 1000; // Form ID to track
    if( $form_data[ 'id' ] != $my_form_id ) {
        return $fields;
    }
     
    $my_field_id = 15; // Hidden field ID to store count
 
    $min_digits = 6; //Minimum digits in counter
     
    // Count the entries and increment the hidden field count by 1 on each submit
    $total_entries = wpforms()->entry->get_entries( array( 'form_id' => $my_form_id ), true );
    $new_total_entries = $total_entries + 1;
     
    // Add leading zeros and update field
    $fields[ $my_field_id ][ 'value' ] = zeroise($new_total_entries, $min_digits);
  
    return $fields;
  
}
add_filter( 'wpforms_process_filter', 'wpf_dev_update_total_field', 10, 3 );

Comments

Add a Comment