php - How do I create a new post upon registration with the users first and last name as title

admin2025-06-02  1

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.

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.

Share Improve this question edited Feb 27, 2019 at 2:00 Dobbs asked Feb 27, 2019 at 1:37 DobbsDobbs 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748861863a314335.html

最新回复(0)