gform_source_id_pre_save_entry

Description

Returns the ID of the post or page where the form submission originated.

Usage

Applies to all forms:

add_filter( 'gform_source_id_pre_save_entry', 'your_function_name', 10, 2 );

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

add_filter( 'gform_source_id_pre_save_entry_FORMID', 'your_function_name', 10, 2 );

Parameters

  • $id Integer|Null
    The ID of the post or page where the form submission originated.
  • $form Form Object
    The form the entry is being created for.

Examples

Custom Post Type

The following shows how you can return the ID of the page where the form submission originated when it belongs to a custom post type, and an Ajax request is being used to save the entry.

add_filter( 'gform_source_id_pre_save_entry', function ( $id, $form ) {
	if ( ! empty( $id ) || ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
		return $id;
	}

	$referer = wp_get_referer();
	if ( empty( $referer ) ) {
		return $id;
	}

	$path = parse_url( $referer, PHP_URL_PATH );
	if ( empty( $path ) ) {
		return $id;
	}

	$page = get_page_by_path( $path, OBJECT, 'your_custom_post_type' );
	if ( empty( $page ) ) {
		return $id;
	}

	return $page->ID;
}, 10, 2 );

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 GFFormsModel::get_source_id() in /forms_model.php

Since

The filter was added in Gravity Forms 2.9