Support » Plugin: Log in with Google » Adding name and surname to registered user

  • Resolved Giovanni Castellotti

    (@giovannicas)


    Is it possible to add support to register the username with the compilation of name and surname fields? Right now the plugin is only registering the email of the user, but not the name and surname of the account. Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Ankit Gade

    (@wpgurudev)

    It’s definitely possible.
    Please add this code to active theme’s functions.php file and it should work for you.

    Consider replacing first name and last name values with appropriate ones.

    
    if ( ! function_exists( 'rt_associate_names' ) ) {
    	/**
    	 * Associate first name and last name to user.
    	 *
    	 * @param int $user_id User ID.
    	 *
    	 * @return void
    	 */
    	function rt_associate_names( int $user_id ): void {
    		wp_update_user(
    			[
    				'ID'         => $user_id,
    				'first_name' => 'some_name',
    				'last_name'  => 'some_other_name',
    			]
    		);
    	}
    
    	add_action( 'rtcamp.google_user_created', 'rt_associate_names' );
    }
    
    Thread Starter Giovanni Castellotti

    (@giovannicas)

    Hi @wpgurudev,
    First of all, thanks so much for the response. I can’t understand what you mean when you say “consider replacing first and last name with appropriate ones”. What I wanted to achieve is that when a user is not registered into the site and the plugin creates an account for him, I also wanted the plugin to take the name and surname from the Google account of the person and put them in the name and last name fields of WordPress. So it should take the full name from the Google account. For this reason I’m asking, what do you mean with “appropriate values”? Thanks a lot

    Ankit Gade

    (@wpgurudev)

    Hi,

    In that case, you can use following code. This should add the first and last name to user profile.

    
    if ( ! function_exists( 'rt_associate_names' ) ) {
    	/**
    	 * Associate first name and last name to user.
    	 *
    	 * @param int       $user_id User ID.
    	 * @param \stdClass $user User object provided by Google.
    	 *
    	 * @return void
    	 */
    	function rt_associate_names( int $user_id, \stdClass $user ): void {
    		wp_update_user(
    			[
    				'ID'         => $user_id,
    				'first_name' => $user->given_name ?? '',
    				'last_name'  => $user->family_name ?? '',
    			]
    		);
    	}
    
    	add_action( 'rtcamp.google_user_created', 'rt_associate_names', 10, 2 );
    }
    
    Thread Starter Giovanni Castellotti

    (@giovannicas)

    Thank you so much, it’s working perfectly. I’ll add a 5 stars review, you deserved it!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.