wppaste
WordPress

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
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.

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

Reaches the database via ->get_col().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
40–145

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 155.

Plugin surface
1 hook

Third-party callbacks on 'remove_user_from_blog' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • sqldatabase query->get_col()called directly
  • cacheobject cachewp_cache_switch_to_blog()one call below remove_user_from_blog()
  • optionoption read or writeget_option()one call below remove_user_from_blog()
  • querycontent queryget_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.

WhenInstructionsCalls it makes
$primary_blog !== false40switch_to_blog(), do_action(), get_user_meta(), get_userdata(), restore_current_blog(), __()
$primary_blog !== false46switch_to_blog(), do_action(), get_user_meta(), get_userdata(), ->remove_all_caps(), get_blogs_of_user(), clean_user_cache(), restore_current_blog()
$primary_blog !== false56switch_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 === false59–67switch_to_blog(), do_action(), get_user_meta(), get_blogs_of_user(), update_user_meta(), update_user_meta(), get_userdata(), restore_current_blog(), __()
$primary_blog === false65–73switch_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 === false75–83switch_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)76switch_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)86switch_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 !== false92switch_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–103switch_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 !== false102switch_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–113switch_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)108switch_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 === false111–119switch_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)118switch_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 === false121–129switch_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–135switch_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–145switch_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:

  1. do_action( remove_user_from_blog )actionline 257 (+18 into the body)

    Fires before a user is removed from a site.

Uses · 10

Used by · 5

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.