add_user_to_blog
Adds a user to a blog.
Description
Use the action to fire an event when users are added to a blog.
Returns (true|WP_Error)
Parameters (3)
- 0. $blog_id (int)
- ID of the blog you're adding the user to.
- 1. $user_id (int)
- ID of the user you're adding.
- 2. $role (string)
- The role you want the user to have
Usage
if ( !function_exists( 'add_user_to_blog' ) ) { require_once ABSPATH . WPINC . '/ms-functions.php'; } // ID of the blog you're adding the user to. $blog_id = -1; // ID of the user you're adding. $user_id = -1; // The role you want the user to have $role = ''; // NOTICE! Understand what this does before running. $result = add_user_to_blog($blog_id, $user_id, $role);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/ms-functions.php
- function add_user_to_blog( $blog_id, $user_id, $role ) {
- switch_to_blog($blog_id);
- $user = get_userdata( $user_id );
- if ( ! $user ) {
- }
- 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
- *
- * @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 );
- wp_cache_delete( $user_id, 'users' );
- wp_cache_delete( $blog_id . '_user_count', 'blog-details' );
- return true;
- }