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
if ( rgar( $entry, '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 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
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.