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.

I've got my user registration to automatically create a post with the user_register hook however the title shows up as (no title) can someone help me with what I'm doing wrong here?

add_action('user_register','create_new_user_post');

$userinfo = get_userdata($user_id);
$disname = $userinfo->display_name;

function create_new_user_post($user_id){
        if (!$user_id>0)return;

        // Create post object
        $my_bio_post = array(
           // 'post_type' => 'ggs_user_post_type',
             'post_title' => $disname,
             'post_content' => 'Put cool content here!',
             'post_status' => 'publish',
             'post_author' => $user_id
        );

        // Insert the post into the database
        $bio = wp_insert_post( $my_bio_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);
}
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Your function create_new_user_post() can't see:

$userinfo = get_userdata($user_id);
$disname = $userinfo->display_name;

Place those within the function so that the function has access to those variables.

share|improve this answer
    
Oh wow, I'm an idiot. Thanks josh and s_ha_dum –  BenRacicot Mar 6 '13 at 15:47

$disname is not set. You define it outside the function's scope. The same is true with $userinfo. And you don't have $user_id set either. It is passed into the function by the add_action mechanism. You need to pull both of those into the callback function as Josh said (and pick his answer. He got here first, and the answer is correct, just not very detailed. Consider this "supplemental".)

function create_new_user_post($user_id){
    if (!$user_id>0) return;

    $userinfo = get_userdata($user_id);
    $disname = $userinfo->display_name;
    // and the rest
share|improve this answer

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.