0

What I'm trying to accomplish is that I'm, at first, creating a new user into my network.

wp_create_user('newuser', 'password', '[email protected]');

At this point, the newuser's role is a subscriber (in network) - now I should move this user into subsite, and change its role as editor. So it would be a subscriber in my network, but an editor in the subsite.

$user = get_user_by('login', 'newuser');
add_user_to_blog( 2, $user->ID, 'editor' );

Logically, this should work, right? But for some reason, the user's role array is empty (in the subsite, in the network its still subscriber as it should be), even that in the subsite's dashboard it shows "Editor". I have also tried to change the role afterward but the role array stays empty.

What I'm missing here?

1
  • How are you getting the roles array exactly? Also did you try all this with all plugins deactivated?
    – Nikolay
    Commented Jun 1, 2018 at 15:01

1 Answer 1

0

Nikolay

Can't comment on my own question, so I guess that giving a loose answer is the best and only way to get back to you.

How I'm getting the roles:

$user = get_user_by('login', 'newuser');
$user_meta = get_userdata($user->ID);
$user_roles = $user_meta->roles;
  • I do not use any plugin. Fresh WP multisite.

I also tried a small trick to get over this:

if(empty($user_roles)) {
    $wp_user_object = new WP_User($user->ID);
    $wp_user_object->add_role('editor');
}

But the roles arrays is still empty and I'm not able to login in the subsite with the moved user.

What makes this interesting is that in the subsite's dashboard you can see that the user is an "Editor"

EDIT: can't comment on my own question because this account is used in different stackexchange forum, and I posted this question as an guest. duh

3
  • Well, I don't know where exactly you are putting the code and at which hooks, and at which site it executes, but overall, this code works on my multisite and the array has either subscriber or editor inside, depending on the site I call from. So I don't know what is going on. By the way you do not need get_userdata since you have the role already from the get_user_by.
    – Nikolay
    Commented Jun 2, 2018 at 16:34
  • @Nikolay interesting... For testing, I'm calling the functions instantly in functions.php file and throw the results on screen with var_dump - In main site the array has subscriber but on subsite its empty. I will try to do some tweaks to my code later today and will see can I get it work.So the code above works on your network without issues? Did you use same functions as I am Commented Jun 3, 2018 at 8:11
  • Try calling the code with the hook wp_loaded to make sure everything needed is loaded. Also try putting the code in a network activated plugin.
    – Nikolay
    Commented Jun 3, 2018 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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