wppaste
WordPress

add_user_to_blog( int $blog_id, int $user_id, string $role ): true|WP_Error

Since
MU (3.0.0)
Source
wp-includes/ms-functions.php:161
Adds a user to a blog, along with specifying the user's role.

Description

Use the 'add_user_to_blog' action to fire an event when users are added to a blog.

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

$blog_idint
ID of the blog the user is being added to.
$user_idint
ID of the user being added.
$rolestring
User role.

Return value

true|WP_Error
True on success or a WP_Error object if the user doesn't exist or could not be added.

Performance profile

How much work a call to add_user_to_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_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
21–63

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

Plugin surface
2 hooks

Third-party callbacks on 'can_add_user_to_blog', 'add_user_to_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 callbacksapply_filters()called directly
  • cacheobject cachewp_cache_delete()called directly
  • querycontent queryget_user_by()one call below add_user_to_blog()

Further down the call graph this can also reach serialize, option 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 · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost add_user_to_blog() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always21switch_to_blog(), get_userdata(), restore_current_blog(), __()
$can_add_user is null, false, long, double, string, array, object, resource && is_wp_error()28switch_to_blog(), get_userdata(), apply_filters(), restore_current_blog(), is_wp_error()
$can_add_user is null, false, long, double, string, array, object, resource && !is_wp_error()35switch_to_blog(), get_userdata(), apply_filters(), restore_current_blog(), is_wp_error(), __()
$can_add_user is not null, false, long, double, string, array, object, resource && get_user_meta()47switch_to_blog(), get_userdata(), apply_filters(), get_user_meta(), ->set_role(), do_action(), clean_user_cache(), wp_cache_delete(), restore_current_blog()
$can_add_user is not null, false, long, double, string, array, object, resource && !get_user_meta()63switch_to_blog(), get_userdata(), apply_filters(), get_user_meta(), update_user_meta(), get_site(), update_user_meta(), ->set_role(), do_action(), clean_user_cache(), wp_cache_delete(), 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: 88 instructions, 21–63 executed per call, 4 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 · 2

2 hooks fire while add_user_to_blog() runs, in this order:

  1. apply_filters( can_add_user_to_blog )filterline 182 (+21 into the body)

    Filters whether a user should be added to a site.

  2. do_action( add_user_to_blog )actionline 211 (+50 into the body)

    Fires immediately after a user is added to a site.

Uses · 13

Show all 13

Used by · 5

Source code

function add_user_to_blog( $blog_id, $user_id, $role ) {	switch_to_blog( $blog_id ); 	$user = get_userdata( $user_id ); 	if ( ! $user ) {		restore_current_blog();		return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );	} 	/**	 * Filters whether a user should be added to a site.	 *	 * @since 4.9.0	 *	 * @param true|WP_Error $retval  True if the user should be added to the site, error	 *                               object otherwise.	 * @param int           $user_id User ID.	 * @param string        $role    User role.	 * @param int           $blog_id Site ID.	 */	$can_add_user = apply_filters( 'can_add_user_to_blog', true, $user_id, $role, $blog_id ); 	if ( true !== $can_add_user ) {		restore_current_blog(); 		if ( is_wp_error( $can_add_user ) ) {			return $can_add_user;		} 		return new WP_Error( 'user_cannot_be_added', __( 'User cannot be added to this site.' ) );	} 	if ( ! get_user_meta( $user_id, 'primary_blog', true ) ) {		update_user_meta( $user_id, 'primary_blog', $blog_id );		$site = get_site( $blog_id );		update_user_meta( $user_id, 'source_domain', $site->domain );	} 	$user->set_role( $role ); 	/**	 * Fires immediately after a user is added to a site.	 *	 * @since MU (3.0.0)	 *	 * @param int    $user_id User ID.	 * @param string $role    User role.	 * @param int    $blog_id Blog ID.	 */	do_action( 'add_user_to_blog', $user_id, $role, $blog_id ); 	clean_user_cache( $user_id );	wp_cache_delete( $blog_id . '_user_count', 'blog-details' ); 	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 7.0.4 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.