Loading...
X

Preserve display_name when updating user data

0 0

This is in the feature list for future updates, but in the meantime you can use this snippet.

add_action('gform_user_updated', 'preserve_displayname', 10, 4);
function preserve_displayname($user_id, $user_config, $entry, $user_pass){
	// apply only to form 91
	if($entry['form_id'] == 91) {
		// get the display name from your hidden field
		// you could also get it from the current user's ID if you don't
		// want to store it in a field in the form.
		$display_name = $entry['17'];

		// the user_id who was logged in and submitted the form
		$user_id = $entry['created_by'];

		// set the wp_users.display_name back to what it was before the User Registration feed processed it
		$result = wp_update_user( array( 'ID' => $user_id, 'display_name' => $display_name ) );

		if (is_wp_error($result)) {
			GFCommon::log_debug("Error when trying to change display name for user #{$user_id} and entry #{$entry['id']}:");
			GFCommon::log_debug(print_r($result,1));
		} else {
			GFCommon::log_debug("Successfully changed display name for user #{$user_id} and entry #{$entry['id']}.");
		}
	}
}

​​Be sure to change the conditional where form ID is 91 to your form ID. This code goes into your theme’s functions.php file.​​

With this code, no matter what the setting in the User Registration Update user feed, the display name will be set back to what is stored in the hidden field in the form (field ID 17 in this case).

Leave Your Observation

Your email address will not be published. Required fields are marked *