does anyone know how to grab first_name and last_name from $user_id for passing into a function firing on the hook user_register?

I am having no luck with either:

function random_name ($user_id){    

$new_user = get_userdata($user_id);
$user_email = $new_user -> user_email;

// get all the meta data of the newly registered user
$new_user_data = get_user_meta($user_id);

// get the first name of the user as a string
$user_firstname = get_user_meta( $user_id, 'first_name', true );

// get the last name of the user as a string
$user_lastname = get_user_meta( $user_id, 'last_name', true );

echo $user_firstname . $user_lastname . $user_email;


I am trying to retrieve the valuesof first name and last name so that I can pass them into an API POST for populating an entery on an external API but when compile the API POST all I get through is the email address?

Thanks in advance.

1 Answer 1


The Codex is always a good place to look for things. Check out this https://codex.wordpress.org/wp_get_current_user

Where you will find this sample code:

    $current_user = wp_get_current_user();
     * @example Safe usage:
     * $current_user = wp_get_current_user();
     * if ( ! $current_user->exists() ) {
     *     return;
     * }
    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />';

Which uses the WP_User object: see https://codex.wordpress.org/Class_Reference/WP_User .

  • Is wp_get_current_user not for checking who is logged in? I am trying to get the first and last name from a newly registered user. I checked the codex for user_register and WP_user object but couldn't see it. Thank you for the comment @Rick Hellewell Feb 19, 2019 at 20:30
  • If you know the user id, you can pass that as a parameter for wp_get_current_user. Otherwise defaults to current user. Feb 20, 2019 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.