How to Create a Unique ID for Each Form Entry

Introduction

Would you like to create a unique ID for each of your form entries? Giving each form entry a unique reference number can make it easier for tracking or any internal function you may want to reference later. Using PHP and WPForms Smart Tags, you can easily achieve this. In this tutorial, we’ll walk you through each step.

For the purpose of this tutorial, we’re going to create a support form for our visitors. Each support submission will be assigned a unique ID, this will be their support ticket number. We’re going to store that number inside the entry on a Hidden Field form field.

Adding the code for the unique ID

Normally we start our tutorials by creating our form, however, since we want to use this Smart Tag inside our form builder, we’re going to start by adding the code snippet to our site first this time. If you need any assistance with adding code snippets to your site, you can read this tutorial for help.

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

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

This snippet will not only create the code we need to build a unique ID with every form submission but it is also allowing us to use this number as a Smart Tag so that we can attach it to our form entries.

Creating your form

Next, we’re going to create our support form. This form will contain fields for Name, Email Address, a Dropdown field for trying to capture the issue, and finally a Paragraph Text form field to allow our visitors a place to give further information about the support they are seeking. However, since we want to store this number as part of our entries, we’ll also be adding a Hidden Field to store that unique ID number as well.

If you need any help in creating a form, please see this documentation.

Once the field is 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.

Hidden fields are not visible inside the form to your visitors, but we’re using this field to store the unique ID for internal reference so that it will display on the entry.

We’re also going to add this Smart Tag to our confirmation message.

Add the unique ID Smart Tag to the confirmation message as well

Now when the form is submitted your visitors will see the unique ID as well as it being recorded inside the Hidden Field of the form.

Now when the form is submitted your visitors will see the unique ID as well as it being recorded inside the Hidden Field of the form.

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.

Snippet References:

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.

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

// 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 );