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:162
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
- Scaling
- Constant
- Instructions
- 21–63
- Plugin surface
- 2 hooks
- Called by
- 5
Reaches the database via get_user_by().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.
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.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_delete()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 21 | switch_to_blog(), get_userdata(), restore_current_blog(), __() |
$can_add_user is null, false, long, double, string, array, object, resource && is_wp_error() | 28 | switch_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() | 35 | switch_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() | 47 | switch_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() | 63 | switch_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:
- apply_filters( can_add_user_to_blog )filterline 183 (+21 into the body)
Filters whether a user should be added to a site.
- do_action( add_user_to_blog )actionline 212 (+50 into the body)
Fires immediately after a user is added to a site.
Uses · 13
- switch_to_blog()Switches the current blog.
- 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.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_user_meta()Retrieves user meta field for a user.
- update_user_meta()Updates user meta field based on user ID.
- get_site()Retrieves site data given a site ID or site object.
- do_action()Calls the callback functions that have been added to an action hook.
- clean_user_cache()Cleans all user caches.
- wp_cache_delete()Removes the cache contents matching key and group.
Show all 13
- WP_Error::__construct()Initializes the error.
Used by · 5
- WP_REST_Users_Controller::create_item()Creates a single user.
- add_existing_user_to_blog()Adds a user to a blog based on details from maybe_add_existing_user_to_blog().
- add_new_user_to_blog()Adds a newly created user to the appropriate blog
- get_active_blog_for_user()Gets one of a user's active blogs.
- wp_initialize_site()Runs the initialization routine for a given site.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.