Generate Unique Submission ID in Hidden Field

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. Include start when sequential is true (defaults to 1 if 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. length is the number of digits (e.g. 848291037).
  • Sequential mode (sequential => true): Suffix is an ascending number. start is the literal first value assigned (e.g. 62602 with length => 6062602, then 062603, …). length controls zero-padding width on output. Do not use a leading zero in PHP for start (e.g. use 62602, not 062602); 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}). Changing start after submissions exist has no effect unless the option is deleted manually.
  • Duplicate rows: Each form_id + field_id pair 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

Snippet Documentation

Frequently Asked Questions

Adding a snippet is easy-peasy.

How do I add a snippet to my WordPress site?

Click the Copy Code button to copy the snippet to your clipboard. Then paste it into the appropriate file in your WordPress installation — typically your theme’s functions.php file or a custom plugin file. Alternatively, you can use a code-snippets plugin to add the code without editing theme files directly.

Where should I place PHP snippets?

The recommended approach is to create a site-specific plugin or use a code-snippets manager plugin. Avoid adding code directly to your theme’s functions.php — those changes are lost when the theme updates. If you must use a theme file, use a child theme’s functions.php instead.

Where should I place JavaScript or CSS snippets?

JavaScript and CSS snippets should be enqueued properly using wp_enqueue_script() or wp_enqueue_style() within a PHP file (such as your custom plugin or child theme’s functions.php). Avoid pasting scripts directly into template files or the Customizer’s “Additional CSS” for anything non-trivial.

Do I need to log in to copy a snippet?

Yes. Snippet code is only visible to logged-in users. Create a free account or log in to view and copy the full code.

What does the “Requires” field mean?

Each snippet lists its dependencies — the minimum versions of WordPress, PHP, and any Gravity Forms add-ons needed for the snippet to work. Make sure your environment meets these requirements before using the code.

Will snippets break my site if I update Gravity Forms?

Snippets rely on Gravity Forms hooks and APIs that may change between major versions. Always check the snippet’s version compatibility before updating. We recommend testing on a staging site first. If a snippet stops working after an update, check back here — we update snippets when breaking changes occur.

Can I modify a snippet to fit my needs?

Absolutely. Snippets are starting points. You’re free to adapt them to your specific use case. If your changes might benefit others, consider contributing back via the linked GitHub repository.