Using WordPress ‘remove_user_from_blog()’ PHP function

The remove_user_from_blog() WordPress PHP function removes a user from a blog.

Usage

To use the remove_user_from_blog() function, provide the user ID, the blog ID (optional), and the reassign user ID (optional).

remove_user_from_blog($user_id, $blog_id = 0, $reassign = 0);

Parameters

  • $user_id (int) – Required. ID of the user being removed.
  • $blog_id (int) – Optional. ID of the blog the user is being removed from. Default is 0.
  • $reassign (int) – Optional. ID of the user to whom to reassign posts. Default is 0.

More information

See WordPress Developer Resources: remove_user_from_blog

Examples

Remove a user from the current blog

This code removes the user with ID 2 from the current blog.

remove_user_from_blog(2);

Remove a user from a specific blog

This code removes the user with ID 3 from the blog with ID 5.

remove_user_from_blog(3, 5);

Remove a user and reassign their posts

This code removes the user with ID 4 and reassigns their posts to the user with ID 6.

remove_user_from_blog(4, 0, 6);

Remove a user from a specific blog and reassign their posts

This code removes the user with ID 7 from the blog with ID 10 and reassigns their posts to the user with ID 8.

remove_user_from_blog(7, 10, 8);

Using the remove_user_from_blog action

This code adds an action that sends an email notification when a user is removed from a blog.

function notify_user_removal($user_id, $blog_id, $reassign) {
    $user = get_userdata($user_id);
    $blog = get_blog_details($blog_id);
    $email_subject = 'You have been removed from ' . $blog->blogname;
    $email_message = 'Hi ' . $user->display_name . ', you have been removed from the ' . $blog->blogname . ' blog.';
    wp_mail($user->user_email, $email_subject, $email_message);
}
add_action('remove_user_from_blog', 'notify_user_removal', 10, 3);