Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a simple requirement:

1. create a user
2. create a role for the user
3. add capability for the role

First, create a new user

$uid = wp_insert_user($userdata);

Second & Third, create a new role and adding capability on it

add_role( $role, $display_name, $capabilities )

How can I make the relationship between the role and the user ?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You should use WP_User class (

It has methods: set_role, add_role, remove_role.


$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 );
share|improve this answer
thanks Krzysiek – diligent Jun 20 '13 at 0:34

Your Answer


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.