remove_user_from_blog( int $user_id, int $blog_id = 0, int $reassign = 0 ): true|WP_Error
- Since
- MU (3.0.0)
- Source
wp-includes/ms-functions.php:239
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.
Compatibility
- WordPress
- since MU (3.0.0)
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$user_idint- ID of the user being removed.
$blog_idintoptional- ID of the blog the user is being removed from. Default 0.Default:
0 $reassignintoptional- ID of the user to whom to reassign posts. Default 0.Default:
0
Return value
true|WP_Error- True on success or a WP_Error object if the user doesn't exist.
Performance profile
How much work a call to remove_user_from_blog() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 40–145
- Plugin surface
- 1 hook
- Called by
- 5
Reaches the database via ->get_col().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 155.
Third-party callbacks on 'remove_user_from_blog' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - sqldatabase query
->get_col()called directly - cacheobject cache
wp_cache_switch_to_blog()one call below remove_user_from_blog() - optionoption read or write
get_option()one call below remove_user_from_blog() - querycontent query
get_user_by()one call below remove_user_from_blog()
Further down the call graph this can also reach serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost remove_user_from_blog() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$primary_blog !== false | 40 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), restore_current_blog(), __() |
$primary_blog !== false | 46 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), clean_user_cache(), restore_current_blog() |
$primary_blog !== false | 56 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), clean_user_cache(), restore_current_blog() |
$primary_blog === false | 59–67 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), restore_current_blog(), __() |
$primary_blog === false | 65–73 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), clean_user_cache(), restore_current_blog() |
$primary_blog === false | 75–83 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), clean_user_cache(), restore_current_blog() |
$primary_blog !== false && empty($post_ids) && empty($link_ids) | 76 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), clean_user_cache(), restore_current_blog() |
$primary_blog !== false && empty($post_ids) && empty($link_ids) | 86 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), clean_user_cache(), restore_current_blog() |
$primary_blog !== false | 92 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
$primary_blog === false && empty($post_ids) && empty($link_ids) | 95–103 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), clean_user_cache(), restore_current_blog() |
$primary_blog !== false | 102 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
$primary_blog === false && empty($post_ids) && empty($link_ids) | 105–113 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), clean_user_cache(), restore_current_blog() |
6 further outcomes, up to 145 instructions
$primary_blog !== false && !empty($post_ids) && !empty($link_ids) | 108 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
$primary_blog === false | 111–119 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
$primary_blog !== false && !empty($post_ids) && !empty($link_ids) | 118 | switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
$primary_blog === false | 121–129 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
$primary_blog === false && !empty($post_ids) && !empty($link_ids) | 127–135 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
$primary_blog === false && !empty($post_ids) && !empty($link_ids) | 137–145 | switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), update_user_meta(), update_user_meta(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), ->prepare(), ->query(), array_walk(), ->prepare(), ->query(), array_walk(), clean_user_cache(), restore_current_blog() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 155 instructions, 40–145 executed per call, 9 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while remove_user_from_blog() runs, in this order:
- do_action( remove_user_from_blog )actionline 257 (+18 into the body)
Fires before a user is removed from a site.
Uses · 10
- switch_to_blog()Switches the current blog.
- do_action()Calls the callback functions that have been added to an action hook.
- get_user_meta()Retrieves user meta field for a user.
- get_blogs_of_user()Gets the sites a user belongs to.
- update_user_meta()Updates user meta field based on user ID.
- get_userdata()Retrieves user info by user ID.
- restore_current_blog()Restores the current blog, after calling switch_to_blog().
- __()Retrieves the translation of $text.
- clean_user_cache()Cleans all user caches.
- WP_Error::__construct()Initializes the error.
Used by · 5
- add_new_user_to_blog()Adds a newly created user to the appropriate blog
- wp_delete_user()Delete user and optionally reassign posts and links to another user.
- wp_uninitialize_site()Runs the uninitialization routine for a given site.
- wpmu_delete_blog()Deletes a site.
- wpmu_delete_user()Deletes a user and all of their posts from the network.
Source code
function remove_user_from_blog( $user_id, $blog_id = 0, $reassign = 0 ) { global $wpdb; $user_id = (int) $user_id; $blog_id = (int) $blog_id; switch_to_blog( $blog_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 = (int) 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' ); } } clean_user_cache( $user_id ); restore_current_blog(); return true;}Changelog
Introduced in MU (3.0.0). Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/ms-functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.