gform_pre_submission_filter

Description

The gform_pre_submission_filter filter is executed after form validation, but before any notifications are sent and the entry is stored. This filter can be used to manipulate the Form Object prior to sending notifications.

Usage

The following would apply the function to all forms.

add_filter( 'gform_pre_submission_filter', 'pre_submission_filter' );

To target a specific form, append the form id to the hook name. (format: gform_pre_submission_filter_FORMID)

add_filter( 'gform_pre_submission_filter_5', 'pre_submission_filter' );

Parameters

ParameterTypeDescription
$formForm ObjectThe form currently being processed.

Examples

Dynamically Populate a Checkbox Field with Published Posts

The following example dynamically populates a checkbox field with a list of published posts.

// NOTE: update the '221' to the ID of your form
add_filter( 'gform_pre_render_221', 'populate_checkbox' );
add_filter( 'gform_pre_validation_221', 'populate_checkbox' );
add_filter( 'gform_pre_submission_filter_221', 'populate_checkbox' );
add_filter( 'gform_admin_pre_render_221', 'populate_checkbox' );
function populate_checkbox( $form ) {
	foreach ( $form['fields'] as &$field ) {
		// NOTE: replace 3 with your checkbox field id
		$field_id = 3;
		if ( $field->id != $field_id ) {
			continue;
		}

		// You can add additional parameters here to alter the posts that are retrieved.
		// More info: https://developer.wordpress.org/reference/functions/get_posts/
		$posts = get_posts( 'numberposts=-1&post_status=publish' );

		$choices  = array();
		$inputs   = array();
		$input_id = 1;

		foreach ( $posts as $post ) {
			// Skipping indexes that are multiples of 10 (multiples of 10 create problems as the input IDs).
			if ( $input_id % 10 == 0 ) {
				$input_id++;
			}
			$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
			$inputs[]  = array( 'label' => $post->post_title, 'id' => "{$field_id}.{$input_id}" );
			$input_id++;
		}

		$field->choices = $choices;
		$field->inputs  = $inputs;
	}

	return $form;
}

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 GFFormDisplay::process_form() in form_display.php.