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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 1 down vote accepted

You should use WP_User class (http://codex.wordpress.org/Class_Reference/WP_User)

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.