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.
![wpforms-increment-count-hidden-field - WPForms create your form and add all your fields including at least one hidden field to store the count for each entry](https://wpforms.com/wp-content/uploads/2021/02/wpforms-increment-count-hidden-field-1024x640.jpg)
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, please review 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 ) {
$my_form_id = 1104;
// Only run on form ID 1104
if( $form_data[ 'id' ] != $my_form_id ) {
return $fields;
}
// since we're in the correct form, this the counter field ID
$my_field_id = 17;
// minimum number of digits required
$min_digits = 6;
// Count the entries so far 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;
// use the WP zeroise function to create the necessary 0s to match the minimum digits required
$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 );
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. Users can customize the functionality by modifying certain variables within the code.
The $my_form_id variable should be set to the specific form ID where this functionality is needed (1104 in the snippet above). The $my_field_id should be adjusted to match the ID of the hidden field that will store the count (17 in the snippet above).
Additionally, the $min_digits variable can be changed to set the desired minimum number of digits for zero-padding the counter value (6 in the snippet above). By adjusting these variables, you can tailor the entry counting and display to your specific form requirements.
If you need any assistance finding your form ID and field ID, please check out this tutorial.
![wpforms-incremented-the-count - WPForms Each entry will now increment the number by one inside the hidden field](https://wpforms.com/wp-content/uploads/2021/02/wpforms-incremented-the-count-1024x593.jpg)
Note: 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
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.