Create posts on user registration

The question:

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)

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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 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)
        //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

and you hook that function using user_register hook


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'

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment