Quick answer: To give every WPForms submission a unique customer or order code, register a custom {unique_number_id} smart tag and use it as a field’s default value. It generates a unique number on each new submission and keeps that same number for the entry afterwards.
What it does
WPForms has smart tags for the date, entry ID, user info and so on, but no “unique code” tag. This adds one. On a new submission it generates a random number; when an existing entry is viewed or emailed later, it returns the number already saved rather than generating a fresh one — so the customer’s code stays stable across the confirmation, the notification email, and the entry record.
The code
Add it as a PHP snippet. Then, in your form, add a field (a hidden field or a read-only text field labelled something like “Order Code”) and set its Default Value to {unique_number_id}.
<?php
/*
* A {unique_number_id} smart tag for WPForms that assigns a unique numeric
* code to each submission. Add it as the Default Value of a hidden or text
* field, e.g. an "Order Code" field.
*/
// 1. Register the smart tag so it appears in WPForms.
function hmwp_register_unique_id_tag( $tags ) {
$tags['unique_number_id'] = 'Unique Number ID';
return $tags;
}
add_filter( 'wpforms_smart_tags', 'hmwp_register_unique_id_tag' );
// 2. Replace the tag with a value: generate one on a new entry, reuse the
// saved one afterwards so the code never changes for an existing entry.
function hmwp_process_unique_id_tag( $content, $tag, $form_data, $fields, $entry_id ) {
if ( 'unique_number_id' !== $tag ) {
return $content;
}
if ( ! $entry_id ) {
// New submission: create the code.
return str_replace( '{unique_number_id}', wp_rand( 1, 5000000000 ), $content );
}
// Existing entry: find the field that held the tag and reuse its value.
if ( ! empty( $form_data['fields'] ) ) {
foreach ( $form_data['fields'] as $field ) {
if ( isset( $field['default_value'] ) && strpos( $field['default_value'], '{unique_number_id}' ) !== false ) {
$field_id = $field['id'];
if ( isset( $fields[ $field_id ]['value'] ) ) {
return str_replace( '{unique_number_id}', $fields[ $field_id ]['value'], $content );
}
}
}
}
return $content;
}
add_filter( 'wpforms_smart_tag_process', 'hmwp_process_unique_id_tag', 10, 5 );
How to use it
- Add the snippet with Code Snippets or WPCode.
- In your form, add a field and set its Default Value to
{unique_number_id}. - Reference that field in your confirmation message and notification email so the customer sees their code.
Notes and how to undo
- The number is random within a large range, which is fine for a human-friendly reference code. If you need a guaranteed-unique sequential number, base it on the WPForms entry ID instead.
- To remove it, delete the snippet and clear
{unique_number_id}from the field’s default value.
