Using WordPress ‘add_new_user_to_blog()’ PHP function

The add_new_user_to_blog() WordPress PHP function is used to add a newly created user to the appropriate blog. This function is specifically hooked into the ‘wpmu_activate_user’ action. For general user addition, consider using add_user_to_blog().

Usage

To use the add_new_user_to_blog() function, you just need to pass in the appropriate parameters:

add_new_user_to_blog($user_id, $password, $meta);

Note: The password is required but it is ignored by this function.

Parameters

  • $user_id (int): The ID of the user you want to add.
  • $password (string): The user’s password. This is required but ignored by this function.
  • $meta (array): An array of metadata for the user.

More information

See WordPress Developer Resources: add_new_user_to_blog()

This function has been implemented in the WordPress Multisite feature and is not deprecated as of the last update.

Examples

Adding a New User

This is how you can add a new user to your blog. In this example, we are assuming the user id is 1 and the meta data is an empty array. The password, although required, is ignored.

add_new_user_to_blog(1, 'password', array());

Adding a New User with Meta Data

In this example, we are adding a user with ID 2 and providing some metadata.

add_new_user_to_blog(2, 'password', array('first_name' => 'John', 'last_name' => 'Doe'));

Adding Multiple Users

You can use this function in a loop to add multiple users. Here we’re adding users with IDs from 3 to 5.

for ($i = 3; $i <= 5; $i++) {
    add_new_user_to_blog($i, 'password', array());
}

Adding User with Meta and Role

In this example, we’re adding a user and setting their role through the metadata.

add_new_user_to_blog(6, 'password', array('role' => 'editor'));

Adding User with Custom Meta Data

In this example, we’re adding a user with some custom metadata.

add_new_user_to_blog(7, 'password', array('custom_field' => 'custom_value'));