function add_user_to_blog( $blog_id, $user_id, $role ) {}

Add a user to a blog.
Use the ‘add_user_to_blog’ action to fire an event when users are added to a blog.


  • int $blog_id: ID of the blog you’re adding the user to.
  • int $user_id: ID of the user you’re adding.
  • string $role: The role you want the user to have

Defined actions

  • add_user_to_blog
    do_action('add_user_to_blog', $user_id, $role, $blog_id);

Source code

function add_user_to_blog( $blog_id, $user_id, $role ) {


	$user = new WP_User($user_id);

	if ( empty( $user->ID ) ) {


		return new WP_Error('user_does_not_exist', __('That user does not exist.'));


	if ( !get_user_meta($user_id, 'primary_blog', true) ) {

		update_user_meta($user_id, 'primary_blog', $blog_id);

		$details = get_blog_details($blog_id);

		update_user_meta($user_id, 'source_domain', $details->domain);



	do_action('add_user_to_blog', $user_id, $role, $blog_id);

	wp_cache_delete( $user_id, 'users' );


	return true;



No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: