WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/add user to blog

Contents

Description

Associates a user to a blog, along with specifying the user's role.

Usage

<?php add_user_to_blog$blog_id$user_id$role ?>

Parameters

$blog_id
(integer) (required) ID of the blog you're adding the user to.
Default: None
$user_id
(integer) (required) ID of the user you're adding.
Default: None
$role
(string) (required) The role you want the user to have
Default: None

Return Value

(True|WP_Error object) 
Returns true on success or a WP_Error object describing the failure

Examples

<?php
//ADD USER ID 1 TO BLOG ID 1 AS AN EDITOR
$user_id = 1; 
$blog_id = 1;
$role = 'editor';
add_user_to_blog( $blog_id, $user_id, $role )
?>

<?php 
//ADD USER ID 2 TO BLOG ID 3 AS AN ADMINISTRATOR
$user_id = 2; 
$blog_id = 3;
$role = 'administrator';
add_user_to_blog( $blog_id, $user_id, $role )
?>

Notes

  • Uses the 'add_user_to_blog' action to fire an event when users are added to a blog.
  • It does not check if the user is already a member of the blog before setting their role. If you don't want to overwrite the role of a user if they are already a member of the blog, use is_user_member_of_blog() to check that first.
  • You do not need to call switch_to_blog() to switch to the blog you want to add the user to before calling this function. The function will switch to the blog itself, and restore the current blog before returning as well.

Source File

add_user_to_blog() is located in wp-includes/ms-functions.php.

Related

Blog User Functions: add_user_to_blog(), add_new_user_to_blog(), remove_user_from_blog(), is_user_member_of_blog()

See also index of Function Reference and index of Template Tags.