Wordpress - wp_insert_user role not working

$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.


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.

Tags:

Php

User Roles