Advertisements

add_user_to_blog

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

Parameters

  • 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 ) {

	switch_to_blog($blog_id);



	$user = new WP_User($user_id);



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

		restore_current_blog();

		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);

	}



	$user->set_role($role);



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

	wp_cache_delete( $user_id, 'users' );

	restore_current_blog();

	return true;

}

383

Advertisements

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:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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: