Using WordPress ‘add_existing_user_to_blog()’ PHP function

The add_existing_user_to_blog() WordPress PHP function adds an existing user to a specific blog in a WordPress Multisite network. It assigns a specified role to the user for that blog.

Usage

Here’s an example of using add_existing_user_to_blog():

$details = array(
    'user_id' => 1, // User ID
    'role' => 'editor' // Role
);
add_existing_user_to_blog($details);

This code will add the user with ID 1 as an editor to the current blog.

Parameters

  • $details (array|false) – User details array. This is optional and defaults to false. The array must contain the following keys:
    • user_id (int) – The ID of the user being added to the current blog.
    • role (string) – The role to be assigned to the user.

More information

See WordPress Developer Resources: add_existing_user_to_blog()

This function is specifically used in WordPress Multisite installations.

Examples

Adding an Editor to Your Blog

$details = array(
    'user_id' => 2,
    'role' => 'editor'
);
add_existing_user_to_blog($details);

This will add the user with ID 2 as an editor to the current blog.

Adding an Author to Your Blog

$details = array(
    'user_id' => 3,
    'role' => 'author'
);
add_existing_user_to_blog($details);

This will add the user with ID 3 as an author to the current blog.

Adding a Contributor to Your Blog

$details = array(
    'user_id' => 4,
    'role' => 'contributor'
);
add_existing_user_to_blog($details);

This will add the user with ID 4 as a contributor to the current blog.

Adding a Subscriber to Your Blog

$details = array(
    'user_id' => 5,
    'role' => 'subscriber'
);
add_existing_user_to_blog($details);

This will add the user with ID 5 as a subscriber to the current blog.

Adding an Administrator to Your Blog

$details = array(
    'user_id' => 6,
    'role' => 'administrator'
);
add_existing_user_to_blog($details);

This will add the user with ID 6 as an administrator to the current blog.