Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

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'
share|improve this answer
    
I was close :) but for some reason I thought It was a bit more complex. Just one thing... there is no mention to the post slug, is there any way to set the slug for post containing in, is, at, and words ? –  user983248 May 25 '12 at 21:45
    
Sorry but this does not actually uses the user info on the post title, or slug :( –  user983248 May 26 '12 at 0:12
    
I updated the answer to show you how to use the user data –  Bainternet May 26 '12 at 13:51
    
@Bainternet, I was trying to use use user_register hook. S2Member was installed there with Firstname, Lastname field enabled. So while user were registering, I could retrieve user fields but not user meta fields like firstname/lastname etc. I've also tried with get_user_meta, but no luck. But those fields were updated in profile fields. Any suggestion? –  Enthusiast Feb 20 '13 at 6:33
    
try setting your hook proiorty to a higher value ex: add_action('user_register','you_callback_function',999); –  Bainternet Feb 21 '13 at 7:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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