You should use WP_User class (http://codex.wordpress.org/Class_Reference/WP_User)
It has methods: set_role, add_role, remove_role.
Example:
$uid = wp_insert_user($userdata);
$u = new WP_User( $uid );
add_role( $role, $display_name, $capabilities ); // I assume $role, $display_name, $caps are already set before
$u->set_role( $role );
Related Posts:
- \WP_User Object | What’s the Difference Between {caps} and {allcaps}?
- Are User Levels Still Currently Used?
- Can I Create a Second Admin Level User Role?
- Assigning multiple or additional capabilities to specific users or how to create additional roles like bbpress roles?
- Editor can create any new user except administrator
- Remove Ability for Other Users to View Administrator in User List?
- Groups of capabilities: users with multiple roles?
- User-edit role setting distinct from wp_capabilities? [closed]
- How to let contributors to create a new revision(draft) editing their published posts
- Disallowing Users of a Custom Role from Deleting or Adding Administrators?
- Grouping users under parent user
- How to update user role without logout
- How to check user role without using current_user_can()
- Do not allow users to create new posts and pages
- How to enable the theme editor cap for an editor role?
- How to assign capabilities to user NOT to User Role
- How-to Delay The Capability To Publish Posts?
- Is there a simple way to manage capabilities per user?
- Get User Role by ID not working
- Getting users by specific capability, not role
- Remove Capabilities from WP admin for specific user role
- Updating wp_user_level on user update
- Add a role and give admin priviledges
- Where are $current_user->allcaps set?
- How can I prevent certain custom roles from seeing other custom roles on the user list page?
- What’s the correct way to add capabilites to user roles?
- Update user role for expired membership
- How to make WordPress ‘editor’ role to list/view/add/edit users only with the role ‘author’?
- Hook into add_user_role and update based on new and removed roles
- How to Moderate Edits to User Profiles?
- fine-grained capabilities for user related capabilities
- Disabling user capability to edit_posts or delete_posts in the front-end
- In admin manage users page, how can I stop users with certain privileges from editing users with other privileges?
- Groups roles & capabilities
- Subscriber role – blank page
- Creating Custom user type just like custom post
- current_user_can() returning true for capability when the user and role do not have the capability
- If the current user is an administrator or editor
- Getting a List of Currently Available Roles on a WordPress Site?
- How to allow an user role to create a new user under a role which lower than his level only?
- user_login vs. user_nicename
- Difference between update_user_meta and update_user_option
- Find out if logged in user is not subscriber
- get_user_meta() doesn’t include user email?
- What’s the difference between the capability remove_users and delete_users?
- Automatically delete inactive users after 2 months
- Check if user is admin by user ID
- Allowing users to edit only their page and nobody else’s
- alphabetically order role drop-down selection in dashboard
- Execute a function when admin changes the user role
- What the user_status column?
- Add Custom User Capabilities Before or After the Custom User Role has Been Added?
- Hide Admin Menu for Specific User ID who has administrator Role
- How to hide media uploads by other users in the Media menu?
- Show admin bar only for some USERS roles
- How to display custom user meta from registration in backend?
- Allowing an email as the username?
- How to create a template for a custom URI associated with the user
- How to order users alphabetically by their last name?
- Extending WP_User class and using this sub-class during the whole lifecycle
- Wp_User_Query not sorting by meta key
- How we can get the user id by its display_name
- WP_User_Query with combined meta query – not working?
- How to use hyperdb to separate and share a user dataset between wordpress installs?
- LEFT JOIN, INNER OUTER JOIN, LEFT OUTER JOIN is driving me crazy. Please help?
- Show Biographical Info while creating new user
- How to programmatically add a user to a role?
- Prevent user creating new users with specific roles
- How to assign an additional/extra/second user-role to multiple users (of a specific user-role)
- How to stop a user from updating the post date
- include user profiles in search results?
- how to add custom user capabilities using add_user_meta or something else?
- How to add follow functionality to multi-author wordpress site?
- BuddyPress | Check if user is in current group [closed]
- Search multiple meta keys at once
- Return ID of authors who have at least one post
- Where to Store Custom User Fields
- Plugin that would allow WordPress Authors to “follow” other Authors and query “Activity Info” from that
- Querying Email Addresses for a List of Users with Same Last Name?
- User profile custom field
- WordPress edit_user_profile_update update secondary role
- Front end user meta options for users
- How do I properly format the user_role array?
- How to restrict specific post types from being read or added by specific user roles (eg. author)?
- ACF Upload Image in front-end with custom form
- How to customize wp_signon()
- Is it possible to get a user with just the password field?
- How to get the Role Name of the current user? (WordPress)
- Prevent Admin user to edit/see super admin from users list
- Hide a user from WordPress
- Redirect after login based on user role (custom login page)
- set_role has no effect
- Drop down list in user profile page
- Check for user meta data at Login
- WP_User->add_role producing unexpected results
- How can I check if the admin bar is visible to the current user?
- Can I create users that have access to *some* other users posts instead of all other users posts?
- Set default page for user account in admin
- Searchable meta value in user.php
- How to add country drop down menu to the user profile?