Hi @hebhansen
Programmatic Way via add_user_to_blog()
If you’re writing a quick PHP script or plugin:
$user_id = 123; // the user ID
$blog_id = 5; // the correct site/blog ID
$wrong_blog_id = 3; // change this value
$role = 'subscriber';
add_user_to_blog( $blog_id, $user_id, $role );
remove_user_from_blog( $wrong_blog_id, $user_id );
Thanks
Ahir Hemant
Thx @hemant-ahir
If I have more tha one, do I seperate by comma? Sorry but I am no superstar in PHP.
$user_id = 15, 22, 123; // the user ID
Thx
Hi @hebhansen
No worries at all — you’re doing great! PHP is picky, so happy to help clear things up 😄
In PHP, you can’t assign multiple values to a variable like this:
$user_id = 15, 22, 123; // ❌ Invalid syntax
Instead, you should use an array if you have multiple user IDs:
$user_ids = [15, 22, 123];
Then you can loop through each user ID like this:
$user_ids = [15, 22, 123];
$blog_id = 5;
$wrong_blog_id = 3;
$role = 'subscriber';
foreach ( $user_ids as $user_id ) {
add_user_to_blog( $blog_id, $user_id, $role );
remove_user_from_blog( $wrong_blog_id, $user_id );
}
That will add all those users to the correct blog and remove them from the wrong one.
Let me know if you want to apply different roles or blog IDs per user — I can help with that too!
Thanks
Ahir Hemant
@hemant-ahir
Ok – Let me get this right:
I prefer to manage users from backend and not by code. So can I fix the above in another way. Point is, in retrospect, what happened is that I set up the site as superadmin as a subdomain. This user was registered. Then I had a go ahead and converted this site to a top level domain. That means I am positive that this user has a cross registration, that somehow conflicts with a lot of other things. Fx she cannot open the FSE Editor etc. So my gut feeling is that something is off and I may need to sort this in DB. I just don’t know where to look and fix this.
Let me hear your thoughts on this before I start hacking in PHP that may not work due to a deeper issue!?
Thx