gform_state_pre_encode

Description

This article explains how to use the gform_state_pre_encode action hook to add values to the form state before it is encoded.

Usage

add_action( 'gform_state_pre_encode', 'my_function', 10, 3 );

Parameters

ParameterTypeDescription
$state_handlerState_HandlerThe state handler instance.
$keysstring|int[]The keys of the state values that will be encoded.
$form_idintThe ID of the form the state is being generated for.

Examples

Add values to the state

add_action( 'gform_state_pre_encode', function ( $state_handler, $keys, $form_id ) {
	$state_handler->add_additional_values( $form_id, array(
		'key1' => 'value1',
		'key2' => array( 'value1', 'value2' ),
	) );

	// or

	$state_handler->add_hashes( $form_id, 'key1', wp_hash( 'value1' ) );
	$state_handler->add_hashes( $form_id, 'key2', array( wp_hash( 'value1' ), wp_hash( 'value2' ) ) );
}, 10, 3 );

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?

Since

This hook was added in Gravity Forms 3.0.

Source Code

This action is found in /class-state-handler.php