0

I've been working at this for some time and for the life of me I cannot figure this out. Here is my code as of now.

add_action('user_register','create_new_user_profile', 999);

function create_new_user_profile($user_id){
    if (!$user_id>0)
            return;

    $user_info = get_userdata($user_id);
    $profile_full_name = $user_info->first_name .  " " . $user_info->last_name;

    $profile_post = array(
         'post_title' => $profile_full_name,
         'post_content' => 'This is the profile post.',
         'post_status' => 'publish',
         'post_type'    => 'Team',
         'post_author' => $user_id
    );

    $profile = wp_insert_post( $profile_post );

}

I've tried several things and searched for some time, the current code returns an empty string, but through other alternatives I've been able to post the username or user_id as the title, but I cant get the display_name or a combination of the first and last name which is what I'm in need of.

1

The get_userdata function is what you are looking for. So for the display_name it would be

$user_info = get_userdata($user_id);
$profile_display_name = $user_info->display_name;

And for first and last name it would be

$full_name = $user_info->first_name .  " " . $user_info->last_name;
  • I've made the additional changes but I still come back with an empty string. I'll edit the original with the changes. – Dobbs Feb 27 at 1:58
  • Is it because the code is taking place during registration and the first and last names are empty when the function fires? I see their first and last names inside their user profile on the back-end. – Dobbs Feb 27 at 2:07
  • The issue was an action that was calling before it ran. Thanks for you help! – Dobbs Feb 27 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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