Description
The State_Handler class generates and validates a hidden ‘state’ value attached to every Gravity Forms form. It prevents submitted values from being tampered with from form display through form submission, and prevents stale submissions from old form loads from being accepted.
The gform_state_lifespan filter allows developers to customize how long that state remains valid before a submission is rejected as stale. By default, this lifespan is set to 2 days (172800 seconds).
You might need to increase the state lifespan if visitors take longer than 2 days to submit a form after loading it. A long form left open in a browser tab, or one filled out across multiple sessions. In that case the submission gets rejected as stale even though nothing was tampered with. Extending the lifespan with this filter prevents false rejection.
Note: it is not recommended increasing the lifespan significantly. A better solution is to exclude the page containing the form from caching or reduce the cache lifetime (TTL) for that page.
Usage
add_filter( 'gform_state_lifespan', 'my_function_name' );
Parameters
| Parameter | Type | Description |
|---|---|---|
$lifespan | int | The state lifespan in seconds. Default is 172800 (2 days). |
$form_id | int | The ID of the form the state is for. |
Examples
Change the state lifespan to 1 week.
add_filter( 'gform_state_lifespan', function () {
return WEEK_IN_SECONDS;
} );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Source Code
This filter is located in Gravity_Forms\Gravity_Forms\Form_Display\State\State_Handler::is_stale() in includes/form-display/state/class-state-handler.php
Since
This filter was added in Gravity Forms 3.1.1