gform_user_registration_update_user_id

Description

This filter can be used to override which user is used to populate the form and updated when the form is submitted.

Usage

The following would apply to all forms with an update type feed.

add_filter( 'gform_user_registration_update_user_id', 'your_function_name', 10, 4 );

To limit the scope of your function to a specific form, append the form ID to the end of the hook name. (format: gform_user_registration_update_user_id_FORMID)

add_filter( 'gform_user_registration_update_user_id_6', 'your_function_name', 10, 4 );

Parameters

  • $user_id integer

    The ID of the user being used to populate the form and being updated on form submission.

  • $entry Entry Object

    The entry currently being processed or an empty array when the form is being rendered.

  • $form Form Object

    The form currently being processed.

  • $feed Feed Object

    The feed currently being processed.

Example

The following example overrides the user ID for form ID 7.

On form display, a URL query string parameter is used to provide the ID, email address (user_email), slug (user_nicename), or login (user_login), of the user whose values should be populated into the mapped form fields. This value should also be populated into a form field. If you don’t want the user to see the value, use a hidden type field.

On submission, the ID of the user to be updated is determined using the value from the form field stored in the $entry. It’s important to note that feeds can be processed in a separate background process that does not have access to the $_POST and $_GET values from form display or submission.

add_filter( 'gform_user_registration_update_user_id_7', function ( $user_id, $entry, $form, $feed ) {
	// Define the property being used to get the user. Supports 'email', 'id', 'slug', or 'login'.
	$key = 'email';

	// Define the name of the query string parameter being used to pass the value to the form on display.
	$query_arg = 'the_URL_param_here';

	// Define the ID of the form field the value was populated into.
	$field_id = '1';

	// Get the value from URL query string on form display or the entry field value during submission feed processing.
	$value = rgar( $entry, $field_id, rgget( $query_arg ) );

	// A value wasn't provided, returning the original ID instead, the ID of the currently logged in user.
	if ( empty( $value ) ) {
		return $user_id;
	}

	// Get the user using the specified key and value.
	$user = get_user_by( $key, $value );

	// If the user exists, return the ID.
	if ( $user ) {
		return $user->ID;
	}

	// The user doesn't exist, returning the original ID instead, the ID of the currently logged in user.
	return $user_id;
}, 10, 4 );

Source

return (int) gf_apply_filters( array( 'gform_user_registration_update_user_id', (int) rgar( $form, 'id' ) ), $user_id, $entry, $form, $feed );

Since version 4.7 this filter is located in GF_User_Registration::get_update_user_id() in class-gf-user-registration.php:

In previous versions, the filter was located in the following methods:

  • GF_User_Registration::update_user() since 3.0.beta1.
  • GF_User_Registration::maybe_prepopulate_form() since 3.0.beta1.3.
  • GF_User_Registration::handle_existing_images_submission() since 3.0.beta1.3.
  • GF_User_Registration::validate() since 3.0.beta1.3.