5

I have be trying this all day but it is not working how do I set a role for the current user I am inserting with the wp_insert_user() function

$userdata = array(
'user_login'    =>  $username,
'user_email'    =>  $email,
'user_pass'     =>  $password,
'user_url'      =>  $website,
'first_name'    =>  $first_name,
'last_name'     =>  $last_name,
'nickname'      =>  $nickname,
'description'   =>  $bio,
'role' => 'Editor'
);

$user = wp_insert_user( $userdata );
3
$WP_array = array (
        'user_login'    =>  $username,
        'user_email'    =>  $email,
        'user_pass'     =>  $password,
        'user_url'      =>  $website,
        'first_name'    =>  $first_name,
        'last_name'     =>  $last_name,
        'nickname'      =>  $nickname,
        'description'   =>  $bio,
    ) ;

    $id = wp_insert_user( $WP_array ) ;

    wp_update_user( array ('ID' => $id, 'role' => 'editor') ) ;

Since you are looking for working solutions, this one should work, and it is only the helpful answer candidate. I am ware that this may not be the best solution, maybe not even close, but it should work.

  • 1
    please explain your code – The Beast Apr 18 '16 at 23:13
  • I copy paste this code from wordpress.org/support/topic/…, will modify it to fit your needs. – prosti Apr 18 '16 at 23:15
  • but what if i want it to be a s2member level 1 – The Beast Apr 18 '16 at 23:26
  • 1
    If you have additional questions please create a new question, it's not fair on prosti to expand the scope indefinitely. The question mentions nothing about s1 membership. @prosti can you edit your answer to explain what you did? Copy paste code isn't an answer, and it doesn't help the OP understand what the problem was – Tom J Nowell Apr 19 '16 at 3:16
  • actually added another answer because of your feedback @TomJNowell – prosti May 11 '16 at 23:48
3

After digging deeper, and checking on my end, I found this script will work just fine.

$userdata = array(
'user_login'    =>  $username,
'user_email'    =>  $email,
'user_pass'     =>  $password,
'user_url'      =>  $website,
'first_name'    =>  $first_name,
'last_name'     =>  $last_name,
'nickname'      =>  $nickname,
'description'   =>  $bio,
'role' => 'editor'
);

$user = wp_insert_user( $userdata );

Yet, there isn't anything mysterious in this script. Looks like the roles need to use lowercase, and you need to blame this function, that I found after days of analysis.

/wp-includes/class-wp-roles.php
284:    /**
285:     * Whether role name is currently in the list of available roles.
286:     *
287:     * @since 2.0.0
288:     * @access public
289:     *
290:     * @param string $role Role name to look up.
291:     * @return bool
292:     */
293:    public function is_role( $role ) {

You can check for your self if you write code like this in functions.php.

$r = new WP_Roles();
var_dump("editor", $r->is_role("editor"));
var_dump("Editor", $r->is_role("Editor"));
die();

You will get the end result like this.

string(6) "editor" bool(true) string(6) "Editor" bool(false) 

This means the editor for the role name is OK and the Editor is not OK.

We must make a note that if you set an empty role like this 'role' => "" this will result in setting no roles at all (like @TheBeast experienced at the first place with Editor name where at the end no role has been set).

On the other hand if you would omit 'role' key then your user will take the default role. The default role is usually the editor, but it is up to you to customize since that info you can get via get_option('default_role'), end set via set_option function.

In the past I found indications that the code from @TheBeast would actually work. (https://wordpress.org/support/topic/wp_insert_user-function-user-roles?replies=3)

What is actually important for us is a process of adding roles. You can write like:

add_action( 'init', 'plugin_add_role');
function plugin_add_role( 'reviewer', 'Reviewer', $caps );

In here your $caps capabilities are something like:

$caps = array(
        'read' => true,
        'edit_posts' => true,
        'edit_others_posts' => true,
        'edit_private_posts' => true,
        'edit_published_posts' => true,
        'read_private_posts' => true,
        'edit_pages' => true,
        'edit_others_pages' => true,
        'edit_private_pages' => true,
        'edit_published_pages' => true,
        'read_private_pages' => true,
    );

You can read more about capabilities in the codex https://codex.wordpress.org/Roles_and_Capabilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.