Description
Generates a human-readable unique ID for each successful form submission and saves it to a designated Hidden field. Supports multiple forms and multiple hidden fields via a config array. Each ID uses a configurable prefix (including any separator) followed by either a random numeric suffix or an ascending numeric suffix.
Usage Notes
- Form setup: Add a standard Hidden field to each target form in the form editor. Note its field ID. No default value or dynamic population is required.
- Configuration: Add one array row per form/field pair. For a single ID on one form, use a single row and remove any extra example rows.
- Required keys per row:
form_id,field_id,prefix(string, may be empty),length,sequential. Includestartwhensequentialistrue(defaults to1if omitted). - Prefix: Include any separator in the prefix value (e.g.
'SUB-','INV-', or''for suffix-only output). - Random mode (
sequential => false): Suffix is a random string of digits only.lengthis the number of digits (e.g.8→48291037). - Sequential mode (
sequential => true): Suffix is an ascending number.startis the literal first value assigned (e.g.62602withlength => 6→062602, then062603, …).lengthcontrols zero-padding width on output. Do not use a leading zero in PHP forstart(e.g. use62602, not062602); padding is applied automatically. - Sequential counter: The last assigned value is stored per field in the WordPress options table (
gf_unique_submission_id_{form_id}_{field_id}). Changingstartafter submissions exist has no effect unless the option is deleted manually. - Duplicate rows: Each
form_id+field_idpair should appear once. Duplicate rows register duplicate hooks and will double-increment or overwrite the same field. - Invalid field: If a row references a missing or non-hidden field, that row logs a debug message and is skipped; other rows and the submission continue normally.
- Concurrent submissions: Sequential mode uses a simple read/increment/write on a WP option — simultaneous submissions could theoretically receive duplicate numbers under heavy load. For high-traffic forms, prefer random mode or add a custom locking strategy.
- Validation retries: Because the hook runs only after validation passes, a user who fails validation and resubmits gets a new ID on the successful attempt (expected for “per submission”). In sequential mode, failed validations do not consume a number.
- Save and continue / drafts: ID is assigned on final submission only; draft entries will not have the ID until completion.
Code
Please log in to view this code snippet.