How to remove a user from a blog with remove_user_from_blog in WordPress multisite

The remove_user_from_blog function in WordPress is designed to remove a user from a specific blog. This function is part of WordPress’s Multisite feature, which allows the management of multiple blogs from a single WordPress installation. By using this function, a user’s capabilities, role, and other connections with a blog can be completely revoked.

From an administrative perspective, this function can be used to manage user access to different blogs in a multisite network. This can be particularly relevant in scenarios where a user no longer needs to have access to a blog, or their role within the blog has changed such that they no longer require the same level of access. It can also be used for security purposes, ensuring that users only have access to the blogs that they are actively involved with.

It is worth noting that the remove_user_from_blog function only removes a user from a specific blog. It does not delete the user’s account or remove their access to other blogs within the multisite network. Therefore, if a user needs to be removed from multiple blogs, the function would need to be run for each blog individually.

Parameters Accepted by the remove_user_from_blog Function

The remove_user_from_blog function in WordPress accepts three parameters which are detailed below:

  • $user_id (int): This obligatory parameter is the ID of the user that is to be removed.
  • $blog_id (int): This optional parameter signifies the ID of the blog from which the user is to be removed. If not specified, the default value is 0.
  • $reassign (int): This is another optional parameter that denotes the ID of the user to whom the posts will be reassigned. If left unspecified, the default value is set to 0.

Return Value of the remove_user_from_blog Function

The remove_user_from_blog function returns either a boolean value or a WP_Error object. If the function executes successfully and the user is removed, it returns true. However, if the user does not exist, it will return a WP_Error object.

Examples

1. How to remove a user from a blog in a multisite WordPress installation

This is the most common usage of the remove_user_from_blog function, removing a user from a blog in a multisite WordPress installation.

$user_id = 2; // The ID of the user you want to remove
$blog_id = 1; // The ID of the blog you want to remove the user from

remove_user_from_blog($user_id, $blog_id);

This code snippet removes the user with the ID of 2 from the blog with the ID of 1.

2. How to remove a user from all blogs in a multisite WordPress installation

If you want to remove a user from all blogs in a multisite WordPress installation, you can do so using a loop and the get_blogs_of_user function in combination with remove_user_from_blog.

$user_id = 2; // The ID of the user you want to remove
$blogs = get_blogs_of_user($user_id);

foreach ($blogs as $blog) {
 remove_user_from_blog($user_id, $blog->userblog_id);
}

This code snippet retrieves all blogs that the user with the ID of 2 is a part of, and then removes the user from each of these blogs.

3. How to conditionally remove a user from a blog in a multisite WordPress installation

You can also use the remove_user_from_blog function conditionally, for example if you only want to remove a user from a blog if they have a certain role.

$user_id = 2; // The ID of the user you want to remove
$blog_id = 1; // The ID of the blog you want to remove the user from
$user = new WP_User($user_id, '', $blog_id);

if (in_array('subscriber', $user->roles)) {
 remove_user_from_blog($user_id, $blog_id);
}

This code snippet checks if the user with the ID of 2 has the role of subscriber on the blog with the ID of 1, and if they do, it removes them from the blog.

Conclusion

The remove_user_from_blog function in WordPress is a tool that allows administrators to remove a user from a blog. This function is primarily used in a multisite network, where a user can be a part of multiple blogs. Utilizing remove_user_from_blog provides an efficient way to manage user roles and access across different blogs within the network, contributing to the overall security and control of the multisite environment.

Related WordPress Functions