On this page
remove_user_from_blog( int $user_id, int $blog_id, int $reassign ): true|WP_Error
Removes a user from a blog.
Description
Use the ‘remove_user_from_blog’ action to fire an event when users are removed from a blog.
Accepts an optional $reassign parameter, if you want to reassign the user’s blog posts to another user upon removal.
Parameters
$user_idint Required-
ID of the user being removed.
$blog_idint Optional-
ID of the blog the user is being removed from. Default 0.
$reassignint Optional-
ID of the user to whom to reassign posts. Default 0.
Return
true|WP_Error True on success or a WP_Error object if the user doesn't exist.
Source
File: wp-includes/ms-functions.php. View all references
function remove_user_from_blog( $user_id, $blog_id = 0, $reassign = 0 ) {
global $wpdb;
switch_to_blog( $blog_id );
$user_id = (int) $user_id;
/**
* Fires before a user is removed from a site.
*
* @since MU (3.0.0)
* @since 5.4.0 Added the `$reassign` parameter.
*
* @param int $user_id ID of the user being removed.
* @param int $blog_id ID of the blog the user is being removed from.
* @param int $reassign ID of the user to whom to reassign posts.
*/
do_action( 'remove_user_from_blog', $user_id, $blog_id, $reassign );
// If being removed from the primary blog, set a new primary
// if the user is assigned to multiple blogs.
$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
if ( $primary_blog == $blog_id ) {
$new_id = '';
$new_domain = '';
$blogs = get_blogs_of_user( $user_id );
foreach ( (array) $blogs as $blog ) {
if ( $blog->userblog_id == $blog_id ) {
continue;
}
$new_id = $blog->userblog_id;
$new_domain = $blog->domain;
break;
}
update_user_meta( $user_id, 'primary_blog', $new_id );
update_user_meta( $user_id, 'source_domain', $new_domain );
}
$user = get_userdata( $user_id );
if ( ! $user ) {
restore_current_blog();
return new WP_Error( 'user_does_not_exist', __( 'That user does not exist.' ) );
}
$user->remove_all_caps();
$blogs = get_blogs_of_user( $user_id );
if ( count( $blogs ) == 0 ) {
update_user_meta( $user_id, 'primary_blog', '' );
update_user_meta( $user_id, 'source_domain', '' );
}
if ( $reassign ) {
$reassign = (int) $reassign;
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) );
if ( ! empty( $post_ids ) ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) );
array_walk( $post_ids, 'clean_post_cache' );
}
if ( ! empty( $link_ids ) ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
array_walk( $link_ids, 'clean_bookmark_cache' );
}
}
restore_current_blog();
return true;
}
Hooks
- do_action( 'remove_user_from_blog',
int $user_id ,int $blog_id ,int $reassign ) -
Fires before a user is removed from a site.
Related
Uses
| Uses | Description |
|---|---|
| get_user_meta() wp-includes/user.php | Retrieves user meta field for a user. |
| get_blogs_of_user() wp-includes/user.php | Gets the sites a user belongs to. |
| update_user_meta() wp-includes/user.php | Updates user meta field based on user ID. |
| switch_to_blog() wp-includes/ms-blogs.php | Switch the current blog. |
| restore_current_blog() wp-includes/ms-blogs.php | Restore the current blog, after calling switch_to_blog() . |
| wpdb::get_col() wp-includes/class-wpdb.php | Retrieves one column from the database. |
| wpdb::query() wp-includes/class-wpdb.php | Performs a database query, using current database connection. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| get_userdata() wp-includes/pluggable.php | Retrieves user info by user ID. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| wp_uninitialize_site() wp-includes/ms-site.php | Runs the uninitialization routine for a given site. |
| wpmu_delete_user() wp-admin/includes/ms.php | Delete a user from the network and remove from all sites. |
| wpmu_delete_blog() wp-admin/includes/ms.php | Delete a site. |
| wp_delete_user() wp-admin/includes/user.php | Remove user and optionally reassign posts and links to another user. |
| add_new_user_to_blog() wp-includes/ms-functions.php | Adds a newly created user to the appropriate blog |
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/remove_user_from_blog