0

I'm trying to create a post when a user registers using the code from here:

Create posts on user registration

The user registers and the post is created BUT the user firstname and user lastname is not working. Each time a post is created just with the title of "Bio"

Here is my code:


function create_new_user_posts($user_id){
        // Get user ID for Post Title
        $user = get_user_by('id', $user_id);
        if (!$user_id>0)
                return;
        // Here we know the user has been created so to create a post we call wp_insert_post
        // Create post object
        $my_bio_post = array(
             'post_title' => $user->user_firstname . " ". $user->user_lastname . 'bio',
             'post_content' => '[li_profile id="author"]',
             'post_status' => 'publish',
             'post_author' => $user_id
        );
        // Insert the post into the database
        wp_insert_post( $my_bio_post );
}
add_action('user_register','create_new_user_posts');

2 Answers 2

0

Check out the documentation for the get_user_by() function - 'user_firstname' and 'user_lastname' aren't properties of the returned object. This is because this data is not a part of the user model - rather they are stored in user metadata.

You should be able to use the get_userdata() function, as it returns an object containing both user properties as well as user metadata.

Alternately, you can use get_user_meta() to retrieve the specific metadata you're looking for:

$first_name = get_user_meta( $user_id, 'first_name', true );
1
  • Regarding not being able to get the user's first and last name in the 'user_register' hook: I don't think the default registration form actually asks for first and last name, and thus they won't be in the user metadata at the time the hook is executed. Have you added first and last name fields to the registration form somehow?
    – bosco
    Commented Dec 3, 2015 at 22:50
0

That makes sense to me but unfortunately it's still only creating posts with the title "Bio". The user meta data is not being found.

Here is the amended code I'm using:

add_action('user_register','create_new_user_posts');

function create_new_user_posts($userid){

    // Get user ID for Post Title;
    $user = get_userdata( $userid );

    if (!$userid>0)
            return;

    // Here we know the user has been created so to create a post we call wp_insert_post
    // Create post object
    $my_bio_post = array(
         'post_title' => $user->first_name . " ". $user->last_name . 'bio',
         'post_content' => '[li_profile id="author"]',
         'post_status' => 'publish',
         'post_author' => $userid
    );

    // Insert the post into the database
    wp_insert_post( $my_bio_post );
}
1
  • 1
    Please don't add additional questions as answers... though I understand why you did so, if you have come back as a different, registered user(?). I'm continuing this conversation in the comments on my answer.
    – bosco
    Commented Dec 3, 2015 at 22:47

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.