gform_user_updated

gform_user_updated

DescriptionUsageParametersExamplesUpdate User RoleUpdate Display NameAdd Additional RolePlacementSource Code

Description
This action is used to trigger an event once a user has been updated through the User Registration Add-On.
Usage
The following would apply to all forms with an update feed.
add_action( 'gform_user_updated', 'your_function_name', 10, 4 );

Parameters

$user_id integer
The ID of the logged in user.

$feed Feed Object
The feed which is currently being processed.

$entry Entry Object
The entry object from which the user was updated.

$user_pass string
The password associated with the user; either submitted by the user or sent via email from WordPress.

Examples
Update User Role
Below is an example that updates the user role based on data submitted on the form.
add_action( 'gform_user_updated', 'change_role', 10, 4 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
//set the role based on data submitted, test form has 3 drop downs with the text true and false as the options
//change this based on your fields/data
$question1 = rgar( $entry, '4' ); //field id 4
$question2 = rgar( $entry, '5' ); //field id 5
$question3 = rgar( $entry, '6' ); //field id 6

if ( $question1 == 'true' && $question2 == 'true' && $question3 == 'true' ) {
//update role
$user_obj = new WP_User( $user_id );
$user_obj->set_role( 'administrator' );
}
}

Update Display Name
The following example shows how you can update the display name using a field value.
add_action( 'gform_user_updated', 'update_display_name', 10, 4 );
function update_display_name( $user_id, $feed, $entry, $user_pass ) {
// get display name from field 2
$display_name = rgar( $entry, '2' );
if ( ! empty( $display_name ) ) {
update_user_meta( $user_id, 'display_name', $display_name );
}
}

Add Additional Role
The following example shows how you can add an additional role to user』s existing role(s).
add_action( 'gform_user_updated', 'add_additional_role', 10, 4 );
function add_additional_role( $user_id, $feed, $entry, $user_pass ) {
// Run only for form id 1
$form = GFAPI::get_form( $entry['form_id'] );
if ( $form['id'] != 1 ) {
return;
}

GFCommon::log_debug( __METHOD__ . '(): running for User ID: ' . $user_id );
// Role name ID to add
$role_to_add = 'custom_role';

// Get current user object
$user_obj = new WP_User( $user_id );
// Add the role above to existing role(s) for the user
$user_obj->add_role( $role_to_add );

GFCommon::log_debug( __METHOD__ . '(): Roles for user ' . $user_id . ': ' . print_r( $user_obj->roles, true ) );

}

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
do_action( 'gform_user_updated', $user_id, $feed, $entry, $user_data['password'] );

This filter is located in GF_User_Registration::update_user() in class-gf-user-registration.php.

gform_user_registration_validation

gform_user_registration_validation

DescriptionUsageParametersExamplesValidate Multi-Site SubmissionIgnore 『This email address is already registered』 error returned by WordPressSource Code

Description
This filter is used to validate submissions that have a User Registration feed attached to the submitted form. It passes several useful variables that make validating submissions dependent on a User Registration feed easier.
This filter is particularly useful if you are adding custom settings/meta to the User Registration feed using the gform_user_registration_add_option_section or gform_user_registration_add_option_group hooks and need to validate the submitted entry based on these settings/meta.
Usage
Applies to all forms.
add_filter( 'gform_user_registration_validation', 'your_function_name', 10, 3 );

Parameters

$form Form Object
The form currently being processed.

$feed Feed Object
The feed which is currently being processed.

$submitted_page integer
The current page number (used to validate only the current page on multi-page forms).

Examples
Validate Multi-Site Submission
This example uses the gform_user_registration_validation filter to validate the User Registration Add-on to add support for Multi-Site.
This example also uses a handy function available in the User Registration add-on which allows you to easily add validation errors to fields by ID: gf_user_registration()->add_validation_error().
add_action( 'gform_user_registration_validation', 'validate_multisite_submission', 10, 3 );
function validate_multisite_submission( $form, $feed, $submitted_page ) {
global $path;

$meta = rgar( $feed, 'meta' );

// make sure multisite create site option is set
if ( rgempty( 'createSite', $meta ) ) {
return $form;
}

// $_POST to Entry
$entry = GFFormsModel::get_current_lead();

$site_address_field = GFFormsModel::get_field( $form, $meta['siteAddress'] );
$site_address = gf_user_registration()->get_meta_value( 'siteAddress', $meta, $form, $entry );

$site_title_field = GFFormsModel::get_field( $form, $meta['siteTitle'] );
$site_title = gf_user_registration()->get_meta_value( 'siteTitle', $meta, $form, $entry );

// get validation result for multi-site fields
$validation_result = wpmu_validate_blog_signup( $site_address, $site_title, wp_get_current_user() );
$error_msg = false;

// site address validation, only if on correct page
if ( $site_address_field->pageNumber == $submitted_page ) {

$error_msg = ( isset( $validation_result['errors']->errors['blogname'][0] ) ) ? $validation_result['errors']->errors['blogname'][0] : false;

if ( $error_msg != false ) {
$form = gf_user_registration()->add_validation_error( $meta['siteAddress'], $form, $error_msg );
}
}

// site title validation, only if on correct page
if ( $site_title_field->pageNumber == $submitted_page ) {

$error_msg = ( isset( $validation_result['errors']->errors['blog_title'][0] ) ) ? $validation_result['errors']->errors['blog_title'][0] : false;

if ( $error_msg != false ) {
$form = gf_user_registration()->add_validation_error( $meta['siteTitle'], $form, $error_msg );
}
}

return $form;
}

Ignore 『This email address is already registered』 error returned by WordPress
If you want to allow users to register on your site using already registered emails, you will need to use the function below along with the gform_user_registration_check_email_pre_signup_activation filter.
add_action("gform_user_registration_validation", "ignore_already_registered_error", 10, 3);
function ignore_already_registered_error($form, $config, $pagenum){

// Make sure we only run this code on the specified form ID
if($form['id'] != 1) {
return $form;
}

// Get the ID of the email field from the User Registration config
$email_id = $config['meta']['email'];

// Loop through the current form fields
foreach($form['fields'] as &$field) {

// confirm that we are on the current field ID and that it has failed validation because the email already exists
if($field->id == $email_id && $field->validation_message == 'This email address is already registered')
$field->failed_validation = false;
}

return $form;

}

Source Code
$form = apply_filters( 'gform_user_registration_validation', $form, $feed, $submitted_page );

This filter is located in GF_User_Registration::validate() in class-gf-user-registration.php.

gform_user_registration_validation_message

gform_user_registration_validation_message

DescriptionUsageParametersExamplesPlacementSource Code

Description
This filter is used to customize the default validation message.
Usage
Applies to all forms.
1add_filter( 'gform_user_registration_validation_message', 'your_function_name', 10, 2 );

Parameters

$message string
The validation message for the current validation error.

$form Form Object
The form currently being processed.

Examples
This example demonstrates how to check which message is passed through the filter and then modifies that message when the correct validation message is found.
123456789add_filter( 'gform_user_registration_validation_message', 'update_validation_msgs', 10, 2 );function update_validation_msgs( $message, $form ) {     if ( $message == 'This username is already registered' ) {        $message = "We're sorry, this username is already registered. Try submitting with a different username.";    }     return $message;}
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
1$field->validation_message = apply_filters( 'gform_user_registration_validation_message', $message, $form );
This filter is located in GF_User_Registration::add_validation_error() in class-gf-user-registration.php.

gform_user_registration_user_meta_options

gform_user_registration_user_meta_options

DescriptionUsageParametersExamplePlacementSource Code

Description
The 「gform_user_registration_user_meta_options」 filter is used by the User Registration Add-On for Gravity Forms. When creating a feed for User Registration, the User Meta has a section titled 「Other User Meta」. The 「gform_user_registration_user_meta_options」 filter allows the options for this group to be set instead of pulling the meta keys from the database. If you do not want the 「Other User Meta」 section displayed at all, return false from this filter.
Usage
add_filter( 'gform_user_registration_user_meta_options', 'your_function_name' );

Parameters

$keys array
An array of meta keys.

Example
add_filter( 'gform_user_registration_user_meta_options', 'add_keys', 10, 1 );
function add_keys( $keys ) {
$keys = array(
array( 'name'=>'test1',
'label'=> 'test1',
'required'=> false),
array( 'name' => 'test2',
'label' => 'test2',
'required' => true )
);
return $keys;
}

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This action is located in GF_User_Registration::get_user_meta_choices() in gravityformsuserregistration/class-gf-user-registration.php.

gform_user_registration_user_data_pre_populate

gform_user_registration_user_data_pre_populate

DescriptionUsageParametersExamples1. Override a field value2. Log the values3. Replace country code4. Unserialize dataPlacementSinceSource Code

Description
This filter can be used to modify a value before it is populated into the field.
Usage
add_filter( 'gform_user_registration_user_data_pre_populate', 'your_function_name', 10, 3 );

Parameters

$mapped_fields array
An array with the field id as the key to the value to be populated into that field.

$form Form Object
The form currently being processed.

$feed Feed Object
The feed currently being processed.

Examples
1. Override a field value
The following example shows how you can override the value for field 12.
add_filter( 'gform_user_registration_user_data_pre_populate', function ( $mapped_fields, $form, $feed ) {
// replace "12" with the field ID you want to replace
$mapped_fields[12] = 'My Custom Value';

return $mapped_fields;
}, 10, 3 );

2. Log the values
add_filter( 'gform_user_registration_user_data_pre_populate', function ( $mapped_fields, $form, $feed ) {

gf_user_registration()->log_debug( 'gform_user_registration_user_data_pre_populate: $mapped_fields => ' . print_r( $mapped_fields, 1 ) );

return $mapped_fields;
}, 10, 3 );

3. Replace country code
add_filter( 'gform_user_registration_user_data_pre_populate', function ( $mapped_fields, $form, $feed ) {
$fields = GFCommon::get_fields_by_type( $form, array( 'address' ) );

if ( ! empty( $fields ) ) {
$codes = GF_Fields::get( 'address' )->get_country_codes();
foreach ( $fields as $field ) {
$country_id = $field->id . '.6';
$value = rgar( $mapped_fields, $country_id );
if ( ! empty( $value ) ) {
$mapped_fields[ $country_id ] = array_search( $value, $codes );
}
}
}

return $mapped_fields;
}, 10, 3 );

4. Unserialize data
add_filter( 'gform_user_registration_user_data_pre_populate', 'gf_unserialize_data', 10, 3 );
function gf_unserialize_data( $mapped_fields, $form, $feed ) {
// replace "12" with the field ID you want to replace
$mapped_fields[12] = maybe_unserialize( $mapped_fields[12] );

return $mapped_fields;
}

Placement
Your code snippet should be placed in the functions.php file of your active theme.
Since
This filter was added in version 2.3.
Source Code
$mapped_fields = apply_filters( 'gform_user_registration_user_data_pre_populate', $mapped_fields, $form, $feed );

This filter is located in GF_User_Registration::prepopulate_form() in class-gf-user-registration.php.

gform_user_registration_update_user_id

gform_user_registration_update_user_id

DescriptionUsageParametersExampleExample 1Example 2Source

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
Example 1
The following example shows how you can override the user id with a specific id.
add_filter( 'gform_user_registration_update_user_id_6', 'override_user_id', 10, 4 );
function override_user_id( $user_id, $entry, $form, $feed ) {

return is_user_logged_in() ? 13 : false;
}

Example 2
The following example overrides the user id form id 7 with the id of the user for the email address supplied in the URL query string when accessing the page containing the form or entered into field id 1 during submission.
add_filter( 'gform_user_registration_update_user_id_7', 'override_user_id', 10, 4 );
function override_user_id( $user_id, $entry, $form, $feed ) {
// Get email address from URL query string on form display or field ID 1 during submission.
$email = rgar( $entry, '1', rgget( 'the_URL_param_here' ) );

return email_exists( $email );
}

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.

gform_user_registration_signup_meta

gform_user_registration_signup_meta

DescriptionUsageParametersExamplePlacementSource Code

Description
The 「gform_user_registration_signup_meta」 filter allows the modification of the signup meta data. This filter is used in the User Registration Add-On for Gravity Forms.
Usage
The following would apply to all forms:
add_filter( 'gform_user_registration_signup_meta', 'your_function_name', 10, 4 );

To target a specific form, append the form id to the hook name. (format: gform_user_registration_signup_meta_FORMID)
add_filter( 'gform_user_registration_signup_meta_74', 'your_function_name', 10, 4 );

Parameters

$meta array
All the metadata in an array (user login, email, password, etc.).

$form Form Object
The current Form object.

$entry Entry Object
The Entry object (to pull the meta from the entry array).

$feed Feed Object
The Feed object.

Example
add_filter( 'gform_user_registration_signup_meta', 'add_blog_template', 10, 4 );
function add_blog_template( $signup_meta, $form, $entry, $feed ) {
$signup_meta['blog_template'] = 1;
return $signup_meta;
}

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GF_User_Registration::handle_user_activation() in gravityformsuserregistration/class-gf-user-registration.php.

gform_user_registration_save_config

gform_user_registration_save_config

DescriptionUsageParametersExamplesSource Code

Description
Used to save custom settings/meta specified using the gform_user_registration_add_option_section or gform_user_registration_add_option_group action hooks.

Removed: This hook was removed in version 3.0 with no replacement. New feed settings defined using gform_userregistration_feed_settings_fields are saved automatically.

Usage
Applies to all forms
1add_filter( 'gform_user_registration_save_config', 'your_function_name' );

Parameters

$config array
The User Registration configuration array.

Examples
This example is a bit of modified code from the User Registration source code that demonstrates how to save the custom option fields added using the gform_user_registration_add_option_section hook to support the MultiSite options.
123456789add_filter( 'gform_user_registration_save_config', 'save_multisite_config' );public static function save_multisite_config ($config ) {     $config['meta']['multisite_options']['create_site'] = RGForms::post("gf_user_registration_multisite_create_site");    $config['meta']['multisite_options']['site_address'] = RGForms::post("gf_user_registration_multisite_site_address");    $config['meta']['multisite_options']['site_title'] = RGForms::post("gf_user_registration_multisite_site_title");     return $config;}
Source Code
This action hook is located in userregistration.php

gform_user_registration_prepared_value

gform_user_registration_prepared_value

DescriptionUsageParametersExamplesCountry CodeUS State CodeCheckboxes as serialized dataPlacementSinceSource Code

Description
This filter can be used to modify a field value before it is saved to the user meta.
Usage
add_filter( 'gform_user_registration_prepared_value', 'your_function_name', 10, 5 );

Parameters

$value string
The value to be modified.

$field Field Object
The field currently being processed.

$input_id string
The ID of the field input currently being processed.

$entry Entry Object
The entry currently being processed.

$is_username bool
Indicates if the current field is mapped to the username.

Examples
Country Code
The following example shows how you can replace the address field country value with the country code.
add_filter( 'gform_user_registration_prepared_value', function ( $value, $field, $input_id, $entry, $is_username ) {
$country_id = $field->id . '.6';
if ( $field->type == 'address' && $input_id == $country_id ) {
return GF_Fields::get( 'address' )->get_country_code( $value );
}

return $value;
}, 10, 5 );

US State Code
The following example shows how you can replace the address field US state value with the state code.
add_filter( 'gform_user_registration_prepared_value', function ( $value, $field, $input_id, $entry, $is_username ) {
$state_id = $field->id . '.4';
if ( $field->type == 'address' && $input_id == $state_id ) {
return GF_Fields::get( 'address' )->get_us_state_code( $value );
}

return $value;
}, 10, 5 );

Checkboxes as serialized data
The following example shows how to serialize values of a checkboxes field before saving to the user meta for a field with id 18.
add_filter( 'gform_user_registration_prepared_value', 'gf_serialize_checkboxes', 10, 5 );
function gf_serialize_checkboxes( $value, $field, $input_id, $entry, $is_username ) {

if ( $field->id == '18' && $field->type == 'checkbox' ) { // Update 18 to your field id number
// Get a comma separated list of checkboxes checked
$checked = $field->get_value_export( $entry );

// Convert to array
$value = explode( ', ', $checked );

// WordPress will automatically serialize this array before saving.
}

return $value;
}

Placement
Your code snippet should be placed in the functions.php file of your active theme.
Since
This filter was added in version 2.1.
Source Code
$value = apply_filters( 'gform_user_registration_prepared_value', $value, $field, $input_id, $entry, $is_username );

This filter is located in GF_User_Registration::get_prepared_value() in class-gf-user-registration.php.