Create posts on user registration

How can I create let say 3 posts after the user finish the registration ?

The ideal outcome would be to create 3 posts using the user as the author of the posts and with some pre-defined values. (post title and slug)

Read More

The post title and slug would be created from info that the user will provide on the registration form.

An example:

User registers and provide Name, Surname, Email (all fields are mandatory)
Three posts are created and the user will be the author of each post.
Each post will have in the title and slug info from the registration form

  • 1- Jon Doe Bio | jon-doe-bio
  • 2- Jon Doe Portfolio | Jon-doe-portfolio
  • 3- Jon Doe Contact | jon-doe-contact

*In the example above it seems unnecessary to have the ability to edit the slug but there will be posts with words like: in, is, at, and, that will be removed otherwise. Also note that the “pre-defined values would be: Bio, Portfolio and Contact.

It would be close to the “welcome” posts created by wordpress after install.

Any idea of how to ?

I know I need to use wp_insert_post( $my_post ); and probably add_action('user_register', 'some_function'); but I don’t know how to create 3 posts in one shot using wp_insert_post( $my_post ); and how to trigger that after the user finish the registration process.

Any idea how to accomplish this ?
Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. You kind of answered the question yourself already,

    Create a function that will create the 3 posts ex:

    function create_new_user_posts($user_id){
            if (!$user_id>0)
                    return;
            //here we know the user has been created so to create 
            //3 posts we call wp_insert_post 3 times.
            // Create post object
            $my_bio_post = array(
                 'post_title' => 'bio',
                 'post_content' => 'This is my post.',
                 'post_status' => 'publish',
                 'post_author' => $user_id
            );
    
            // Insert the post into the database
            $bio = wp_insert_post( $my_bio_post );
            $my_portfolio_post = array(
                 'post_title' => 'portfolio',
                 'post_content' => 'This is my post.',
                 'post_status' => 'publish',
                 'post_author' => $user_id
            );
    
            // Insert the post into the database
            $portfolio = wp_insert_post( $my_portfolio_post );
            $my_contact_post = array(
                 'post_title' => 'bio',
                 'post_content' => 'This is my post.',
                 'post_status' => 'publish',
                 'post_author' => $user_id
            );
    
            // Insert the post into the database
            $contact = wp_insert_post( $my_contact_post );
    
            //and if you want to store the post ids in 
            //the user meta then simply use update_user_meta
            update_user_meta($user_id,'_bio_post',$bio);
            update_user_meta($user_id,'_portfolio_post',$portfolio);
            update_user_meta($user_id,'_contact_post',$contact);
    }
    

    and you hook that function using user_register hook

    add_action('user_register','create_new_user_posts');
    

    Update
    When you hook a function to user_register the function receives the user id so you can use that to get whatever information you want about that user ex:

    $user = get_user_by('id', $user_id);
    

    and now $user is a USER object so you can change the post title to user the user info ex:

    'post_title' => $user->user_firstname . " ". $user->user_lastname . 'bio'