0

Can anyone help me to create a custom post type content when a new user is registered? But the user should be of specific role only.

I have been searching but couldn't find any solution.

1 Answer 1

0

Try below code:

add_action( 'user_register', 'create_new_post_onuser_registration', 10, 1 );
function create_new_post_onuser_registration( $user_id ){
    // Get user info
    $user_meta = get_userdata( $user_id );

    $user_roles = $user_meta->roles;

    // Update the role here
    if ( in_array( 'subscriber', $user_roles ) ) {
        // Do something.

        // Create a new post
        $subscriber_post = array(
            'post_title'   => $user_meta->nickname;
            'post_content' => $user_meta->description,
            'post_type'    => 'custom_post_type', // Update to your custom post type
        );

        $post_id = wp_insert_post( $subscriber_post );

        // Add custom data
        add_post_meta( $post_id, 'user_email', $user_meta->user_email );
    }    
}

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.