How to Create a Unique ID for Each Form Entry

Overview

Would you like to create a unique ID for each of your form entries? If you’d like to give each form entry a unique reference number, you can easily achieve this with PHP. We’ll show you how to create a unique ID for each form submission.

Setup

In the steps below, we’ll first create a custom Smart Tag and then add that custom tag to the email notification and the confirmation message.

1) Adding the PHP

The code shown below will create a new Smart Tag called {unique_id} which will create a unique ID number for each form submission.

Just copy the code to your site.

/*
 * Create a unique_id Smart Tag and assign it to each form submission.
 *
 * @link https://wpforms.com/developers/how-to-create-a-unique-id-for-each-form-entry/
 *
 */
function wpf_dev_process_smarttag( $content, $tag ) {

	// Only run if it is our desired tag.
	if ( 'unique_id' === $tag ) {
		
		// Replace the tag with value.
		$content = str_replace( '{unique_id}', uniqid(), $content );
	}

	return $content;
}
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );

2) Adding the Hidden Field

Next, we’re going to add the new Smart Tag to a hidden field so it will be included in the email confirmation.

To add this new Smart Tag to a field, you’ll need to open the form builder and add a Hidden Field form field.

Once added, open the Field Options panel, and go to the Advanced Options section.

In the Default Value just add the Smart Tag {unique_id}.

Add the new Smart Tag to assign the unique ID to each form entry.

3) Editing the Confirmation Message

Finally, we’re going to add the new Smart Tag to the email confirmation message.

Under the form Settings » Confirmation, you can add the Smart Tag {unique_id}.

Add the unique_id Smart Tag to the confirmation message

When the form is submitted, the users will see the new Smart Tag in the confirmation message and inside their email notification.

Now the unique ID will show in the confirmation message and email notification.

And that’s all you need to create a unique ID for each form submission. Would you like to process a Smart Tag inside Checkbox field labels? Try out our tutorial on How to Process Smart Tags in Checkbox Labels.

FAQ

Q: What if I wanted a specific number of characters for my unique ID?

A: You can see the following example will only provide a 6 digit (numeric only) unique ID.

///Generate Unique ID Smart Tag for WPForms
function wpf_dev_register_smarttag( $tags ) {

	// Key is the tag, item is the tag name.
	$tags['unique_id'] = 'Unique ID';

	return $tags;
}
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
//Generate Unique ID value
function wpf_dev_process_smarttag( $content, $tag ) {

	// Only run if it is our desired tag.
	if ( 'unique_id' === $tag ) {
		//generate unique ID
		$unique_id = str_pad(mt_rand(1,999999),6,'0',STR_PAD_LEFT);
		// Replace the tag with our Unique ID.
		$content = str_replace( '{unique_id}', $unique_id, $content );
	}

	return $content;
}
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );