Using WordPress ‘remove_user_from_blog’ PHP action

The remove_user_from_blog WordPress PHP action fires before a user is removed from a site.

Usage

add_action('remove_user_from_blog', 'my_custom_function', 10, 3);

function my_custom_function($user_id, $blog_id, $reassign) {
    // Your custom code here
}

Parameters

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

More information

See WordPress Developer Resources: remove_user_from_blog

Examples

Log user removal

Log when a user is removed from a blog.

add_action('remove_user_from_blog', 'log_user_removal', 10, 3);

function log_user_removal($user_id, $blog_id, $reassign) {
    error_log("User ID {$user_id} has been removed from blog ID {$blog_id}.");
}

Notify user when removed

Send a notification email to the user being removed from the blog.

add_action('remove_user_from_blog', 'notify_user_removal', 10, 3);

function notify_user_removal($user_id, $blog_id, $reassign) {
    $user_data = get_userdata($user_id);
    $blog_name = get_bloginfo('name', 'display');
    $subject = "Removed from {$blog_name}";
    $message = "You have been removed from the {$blog_name} blog.";
    wp_mail($user_data->user_email, $subject, $message);
}

Remove user from a custom table

Remove the user from a custom table when they are removed from the blog.

add_action('remove_user_from_blog', 'remove_user_from_custom_table', 10, 3);

function remove_user_from_custom_table($user_id, $blog_id, $reassign) {
    global $wpdb;
    $wpdb->delete($wpdb->prefix . 'my_custom_table', array('user_id' => $user_id), array('%d'));
}

Reassign user’s custom data

Reassign the removed user’s custom data to the specified user.

add_action('remove_user_from_blog', 'reassign_user_custom_data', 10, 3);

function reassign_user_custom_data($user_id, $blog_id, $reassign) {
    global $wpdb;
    $wpdb->update($wpdb->prefix . 'my_custom_data', array('user_id' => $reassign), array('user_id' => $user_id), array('%d'), array('%d'));
}

Remove user’s role from another blog

Remove the user’s role from another related blog when they are removed from the current blog.

add_action('remove_user_from_blog', 'remove_user_from_another_blog', 10, 3);

function remove_user_from_another_blog($user_id, $blog_id, $reassign) {
    $another_blog_id = 2; // The ID of the other blog
    remove_user_from_blog($user_id, $another_blog_id, $reassign);
}