How to Increment a Count on Each Form Submission

Overview

Would you like to increment a count on each form submission inside a Hidden Field? You can easily keep track of the number of entries for your form by adding a hidden field to your form that will store the count and then a small PHP snippet that will create the count for you. In this tutorial, we’ll show you how to achieve this.

Setup

To achieve an active count inside a Hidden Field on your form, you must first add a Hidden Field to your form so that the entry count can be stored there.

It’s important to note that if for any reason, you would delete a form entry, this will throw off your count and could cause duplicate numbers on your count.

Add a hidden field to your form

Next, add this code to your site.

/**
 * 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 ) {

	// Only run on my form with ID = 817
	if( $form_data[ 'id' ] != 817 ) {
		return $fields;
	}

	// Count the entries so far and increment the hidden field count by 1 on each submit
	$fields[4]['value'] = wpforms()->entry->get_entries( array( 'form_id' => 817 ), true ) + 1;

	return $fields;
}
add_filter( 'wpforms_process_filter', 'wpf_dev_update_total_field', 10, 3 );

You’ll need to update the code above to replace the form ID 817 and the Hidden Field ID $fields[4] to match your own form and field ID numbers.

Now you’ll see when you view the entry, each form entry increments the count by one upon each submission.

Each entry will now increment the number by one inside the hidden field

And that’s all you need to increment a count on each form entry. Would you like to find out other ways in which you can use the Hidden Field? Such as assigning a unique ID number on each submission? Take a look at our article on How to Create a Unique ID for Each Form Entry.