0

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);
}

2 Answers 2

1

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.

0
0

$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

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.