Generate unique customer/User code – wpForms

WordPress PHP July 23, 2026
/*
 * 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 );