How to Create a Unique ID for Each Form Entry

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 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.

There is already a default Smart Tag in WPForms that will generate a unique value. However, the purpose of this tutorial is to control more what that unique value may be such as limiting to numbers only, adding a prefix before the unique ID, etc. For further information on the Unique Value Smart tag, please see this helpful guide.

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.

With any browser caching, server caching, or plugin caching there is a chance these unique IDs will duplicate.

Creating 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, $form_data, $fields, $entry_id ) {
          
    // Only run if it is our desired tag.
    if ( 'unique_id' === $tag && !$entry_id ) {
		
        // Replace the tag with our Unique ID that will be prefixed with wpf.
        $content = str_replace( '{unique_id}', uniqid('wpf', true), $content );
    } elseif ( 'unique_id' === $tag && $entry_id ) {
		
		foreach ($form_data[ 'fields' ] as $field) {
			
			if ( preg_match('/\b{unique_id}\b/', $field[ 'default_value' ]) ) {
				$field_id = $field[ 'id' ];
				break;
			}
			
		}
		
        $content = str_replace( '{unique_id}', $fields[$field_id][ 'value' ], $content);
    }
  
    return $content;
}
  
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 5 );

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.

Adding the unique ID to the confirmation message

If you’d like to add your unique ID to be part of your confirmation message, you’ll need to place the value that is stored inside the Hidden Field when the form is submitted.

In our form, the field ID was 6 as you can see in the previous image, so we’re going to add this to our confirmation message so it will pull in that exact same value.

{field_id="6"}

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

If you need help with how to find your field ID, please check out this tutorial.

When using the unique ID inside the confirmation message, you must use this field ID inside the message as pictured above and not the actual Smart Tag.

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.

Reference Filters

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 with a specific number of characters 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, $form_data, $fields, $entry_id ) {
          
    // Only run if it is our desired tag.
    if ( 'unique_id' === $tag && !$entry_id ) {
		
    // generate a hexadecimal string based on the time to ensure uniqueness
    // reduce the string to 6 characters
    $uuid = substr(md5(time()), 0, 6);
		
        // Replace the tag with our Unique ID.
        $content = str_replace( '{unique_id}', $uuid, $content );
    } elseif ( 'unique_id' === $tag && $entry_id ) {
		
		foreach ($form_data[ 'fields' ] as $field) {
			
			if ( preg_match('/\b{unique_id}\b/', $field[ 'default_value' ]) ) {
				$field_id = $field[ 'id' ];
				break;
			}
			
		}
		
        $content = str_replace( '{unique_id}', $fields[$field_id][ 'value' ], $content);
    }
  
    return $content;
}
  
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 5 );

This snippet will grab the current time and convert it into a hexadecimal string and restrict it to a 6-character number.

Q: Can I get use this snippet to get a numeric-only value?

A: Of course! You can use this snippet that will return a unique numeric value between the numbers of 1 – 5,000,000,000. Using this number range will mean you’ll end up with a unique number that is between 1 and 10 digits. If you want to reduce the number of digits generated, you would reflect that in the rand(1, 5000000000).

For example, if you only wanted a 6 digit number, the snippet would be something like rand(100000, 999999), but this does increase the possibility of repeating numbers eventually since there are so many different combinations you can use with a 6 digit limit.

/*
 * Create a unique_id numeric-only 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_number_id' ] = 'Unique Number ID';
  
    return $tags;
}
  
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
  
// Generate Unique ID value
function wpf_dev_process_smarttag( $content, $tag, $form_data, $fields, $entry_id ) {
           
    // Only run if it is our desired tag.
    if ( 'unique_number_id' === $tag && !$entry_id ) {
         
        // Replace the tag with our Unique ID.
        $content = str_replace( '{unique_number_id}', rand(1, 5000000000), $content );
    } elseif ( 'unique_number_id' === $tag && $entry_id ) {
         
        foreach ($form_data[ 'fields' ] as $field) {
             
            if ( preg_match('/\b{unique_number_id}\b/', $field[ 'default_value' ]) ) {
                $field_id = $field[ 'id' ];
                break;
            }
             
        }
         
        $content = str_replace( '{unique_number_id}', $fields[$field_id][ 'value' ], $content);
    }
   
    return $content;
}
   
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 5 );

Q: Can I prefix the unique ID?

A: Absolutely, if you’d like to add a prefix, you can use this snippet.

/*
 * Create a unique ID and add a prefix.
 *
 * @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[ 'my_unique_id' ] = 'My Unique ID';
 
    return $tags;
}
 
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
 
// Generate Unique ID value
function wpf_dev_process_smarttag( $content, $tag, $form_data, $fields, $entry_id ) {
           
    // Only run if it is our desired tag.
    if ( 'my_unique_id' === $tag && !$entry_id ) {
         
        // Replace the tag with our Unique ID that will be prefixed with wpf.
        $content = str_replace( '{my_unique_id}', uniqid('WPF-', true), $content );
    } elseif ( 'my_unique_id' === $tag && $entry_id ) {
         
        foreach ($form_data[ 'fields' ] as $field) {
             
            if ( preg_match('/\b{my_unique_id}\b/', $field[ 'default_value' ]) ) {
                $field_id = $field[ 'id' ];
                break;
            }
             
        }
         
        $content = str_replace( '{my_unique_id}', $fields[$field_id][ 'value' ], $content);
    }
   
    return $content;
}
   
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 5 );

Q: Can I control the number by incrementing the count?

A: Absolutely, however, if you would like to increment the number we recommend following this useful guide instead.