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
- How to allow an user role to create a new user under a role which lower than his level only?
- What’s the difference between the capability remove_users and delete_users?
- Automatically delete inactive users after 2 months
- Allowing users to edit only their page and nobody else’s
- alphabetically order role drop-down selection in dashboard
- How to hide media uploads by other users in the Media menu?
- How to order users alphabetically by their last name?
- Wp_User_Query not sorting by meta key
- How to use hyperdb to separate and share a user dataset between wordpress installs?
- Show Biographical Info while creating new user
- Prevent user creating new users with specific roles
- include user profiles in search results?
- Return ID of authors who have at least one post
- Plugin that would allow WordPress Authors to “follow” other Authors and query “Activity Info” from that
- User profile custom field
- Front end user meta options for users
- Prevent Admin user to edit/see super admin from users list
- Drop down list in user profile page
- How can I check if the admin bar is visible to the current user?
- Set default page for user account in admin
- Searchable meta value in user.php
- User role permissions based on taxonomies
- Hide everything on site for visitors except specific page IDs
- current user can edit user?
- the_author_meta(‘user_url’, $author->ID) not working properly. how can I solve this?
- How to set show admin bar front to true for all users?
- Add user data to table when user is created?
- Restricted registrations or removing the ability to edit your password/email
- Use wp_update_user to update custom column in wp_users table
- Display users in order by an “order” custom meta field
- Does an AJAX call on the Admin Side Automatically include the User Data/Capabilities?
- Rest API code to get ID of current user not working: get_current_user_id() gives 0
- How can I attach a user to another user with user meta
- How to get a users list by who created them?
- Does wordpress support natively the concept of logging-in users? (not admins, but users of the website)
- How to pull all the contributer users records and order by Designation (which is users meta data)?
- cannot get user_registered date from get_user_meta
- How many people are allowed to use same wordpress account at the same time?
- How to hide user profile fields based on the role of the viewed user?
- Customising “user ids” and add to ‘user’ panel in the admin area
- How to hide some users to unlogged users [closed]
- List all users and current week entries
- Redirect admin 403 “Cheatin uh?” admin pages
- Privilege to recover trashed posts
- User Meta stuff
- User can’t search himself on rest api
- Allow a user or role to view drafts and previews, but not other admin privileges?
- How do I update user email from frontend input field?
- current_user_can() causing critical error
- Author Error “Sorry, you are not allowed to access this page.”
- How can i add user display name drop down menu in frontend?
- How to prevent multiple user accounts with the same meta field?
- Create relationships between users or user roles
- Add another role to a user when they click a button?
- User Permissions on custom post type
- WordPress install checking permissions of user id 0
- Allowing users to edit only their page and nobody else’s
- Store extra user values permanently
- Limit roles displayed in users.php depending on custom role
- Sort users by custom user meta value
- Control Category of each user can post
- Extend user search in the users.php page to allow for searching by role and excluding specified email domains from the “users search” input box
- How can I list users by date in an array in meta_value?