Multisite User Dynamic Creation only for that site without plugin!

I am using the following code to create a user on a multisite like so:

$password = wp_generate_password(12, false);
$created_user = wp_create_user($_POST['email'], $password, $_POST['email']);

The generated password gets emailed to them using wp_mail() later on down in the code.

I have a custom role called, family that I assign like so if !is_wp_error($created_user):

$blog_id = get_current_blog_id();
add_user_to_blog($blog_id, $created_user, 'family');

This works for adding this user to the current blog dynamically, however, the problem with this is that I don’t want that same user to be able to login to other sites. They should not have access to other sites on the network. How to remove access to other sites for this same user? Because, I’ve just noticed that this user has the ability to login to other sites.

I understand that wordpress adds this user as a subscriber by default on the network for all sites, but I need to remove this functionality, and still keep my code in place. I’d rather not use a plugin to do this.

How to do this, need to disable login access for all sites except the $blog_id that the user has been added to as family role. Searching for this online does not provide any good help on how to do this.

Read more here: Multisite User Dynamic Creation only for that site without plugin!

Leave a Reply

Your email address will not be published. Required fields are marked *