How to Increment a Count on Each Form Submission

Would you like to increment a count on each form submission inside a Hidden Field? You can easily stamp each entry with an increment count to track the number of entries in order on each entry. In this tutorial, we’ll start 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.

Creating the form

To begin, we’ll create a new form. Add all the relevant fields to your form and then before you save the form, be sure to add a Hidden Field. This field will store the number as each entry is submitted.

create your form and add all your fields including at least one hidden field to store the count for each entry

For any assistance in creating your form, please review this documentation.

Incrementing a count

Next, you’ll need to add the code snippet that will assign the count to each form submission.

If you need help in adding code snippets to your site, pleasereview this tutorial.

/**
 * 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[ 9 ][ '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 );

This snippet will look at the total count you have currently for your form entries, if this is a new form, the count will start with 1. However, if you’re implementing this after you’ve already received some form entries, it will count the entries you have first and then increment the count by +1 for each submission thereafter. For instance, on a form that has 10 entries, the number assigned would be 11, but on a brand new form, the number assigned would be 1.

Also, note that this snippet will only run on the form ID 817. You’ll need to update this number to match your own form ID.

The $fields[ 9 ] will need to be updated to match the field ID you have for the Hidden Field.

If you need any assistance finding your form ID and field ID, please check out this tutorial.

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

You don’t need to add this just to a Hidden Field, you can easily use this inside an email notification or in a confirmation message. To use the field, just place {field_id=”9″} inside the message on the Confirmations tab and/or the Notifications tab.

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.

Reference Filter

wpforms_process_filter

FAQ

Q: Can I create more than just a number for the count?

A: Absolutely! Here’s an example of adding some letters in front of the count. We’re going to add abc- in front of our count.

/**
 * Increment total entry number on each submission with formatted letters 
 *
 * @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
    $entry_count = wpforms()->entry->get_entries( array( 'form_id' => 917 ), true ) + 1;
    $formatted_count = sprintf('%03d', $entry_count);
    $fields[ 9 ][ 'value' ] = 'abc-' . $formatted_count;

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

Q: Can I assign a prefix to this number?

A: Absolutely! To add a prefix to your incremental number, you can easily use this snippet.

/**
 * Increment total entry number with a prefix 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[ 3 ][ 'value' ] = 'WPF-' . wpforms()->entry->get_entries( array( 'form_id' => 817 ), true ) + 1;
 
    return $fields;
 
}
add_filter( 'wpforms_process_filter', 'wpf_dev_update_total_field', 10, 3 );

On a brand new form, the number assigned to the Hidden Field would be WPF-1. However, on a form that already had 3 entries previously, the number assigned would be WPF-4.