WordPress Multisite: How to Add a User to All Sites
Creating users with appropriate roles is one of the most important parts of managing a multisite network. Occasionally, you may need to add a user to all sites in the WordPress multisite network.
When that happens, you have 2 options: manually add the same user to every site in the network or use a custom function to automate the process.
Let’s find out how to do both in this step-by-step guide.
How to Add a WordPress Multisite User to All Sites
Method #1: Add a User Manually to All Sites
The first method for adding a user to all sites in a multisite network is manually adding the user to every site.
On the dashboard, go to My Sites > Network Admin > Sites.
Once on the Sites screen, click on the subdomain to which you plan to add the user.
On the Edit Site screen, click on the Users tab.
Now, you can add an existing user or create a new one and assign it to this particular site.
In the Add Existing User section, you need to type the username, pick the user’s role, and click Add User.
If you instead want to create a new user and add it to the site, scroll down to the Add New User section. Create the username, assign an email address for the user, give it a role, and click on Add New User.
You need to repeat this process on every site in the network if you want to add a user to all sites.
Method #2: Add an Administrator to All Sites With a Function
Adding users manually to every site can be feasible if you manage a handful of sites, but when you manage a large network, the time it takes to add new users becomes significant. Even in smaller networks, going to every site to add multiple users gets old fast.
If you have some coding knowledge, you may benefit from adding a custom function that allows you to enter an email address and turn it into a user on every site in the network.
To programmatically add admin users to all sites in the network, add the following function, created by developer Kevin Leary, to the
file.functions.php
/**
* WPMU Add User to All Sites
*
* Add an administrator user to all sites in a WordPress multisite network
*/
function kevinlearynet_wpmu_add_user_all_sites() {
$email = isset( $_GET['wpmu_admin_sync'] ) ? esc_attr( $_GET['wpmu_admin_sync'] ) : null;
if ( ! $email ) {
return;
}
$user = get_user_by( 'email', $email );
if ( $user === false ) {
wp_die( "No user exists in this network with the email $email." );
}
$role = isset( $user->roles[0] ) ? $user->roles[0] : 'administrator';
$sites = get_sites( [
'limit' => 0,
'public' => true,
'spam' => false,
'deleted' => false,
'archived' => false,
'mature' => false,
] );
$output = [];
foreach ( $sites as $site ) {
$site_id = get_object_vars( $site )['blog_id'];
$site_name = get_blog_details( $site_id )->blogname;
$add_user = add_user_to_blog( $site_id, $user->ID, $role );
$note = "$email user to $site_name as an $role.";
$output[] = $add_user ? "Successfully added $note" : "Failed adding $note";
}
$html = wpautop( implode( PHP_EOL, $output ) );
wp_die( $html );
}
add_action( 'after_setup_theme', 'kevinlearynet_wpmu_add_user_all_sites' );
How to Use the Function?
To use the function, start by visiting your multisite network. Add the following query parameter to your network’s URL:
?wpmu_admin_sync=[user_name_here]@[domain-name_here].com
Here’s an example:
mysite.com/?wpmu_admin_sync=adminuser@mysite.com
Once you’ve added the query parameter with the email address, press Enter. The script will add a new user based on the email address to every site in the network.
After running the function, if you go to Sites > All Sites, and click on any of the sites, the go to the Users tab, you should see the newly added admin user.
Keep in mind that the email address you add must already be associated with an existing user. If you add an email address that isn’t associated with an existing user, you will see this error message.
How Does the Function Work?
This function adds a user by email to all sites in the network. It creates a list of all sites, finds an existing user by mail, and adds it to every site one by one.
The function adds a user with the administrator role by default, so you should modify it to add any other type of user.
Final Thoughts
Managing users is very important in multisite networks. Occasionally, it may be necessary to add a user to every site in the network, but WordPress doesn’t provide a native feature to automatically add the same user to every site.
Your alternatives are to manually add the same user to every site or to use a function that automates the process.
This article provided a step-by-step guide to do both, so now you know what to do next time you have to add a user to every site in your network.
If you found this post useful, read our blog and resources for more WordPress insights and guides!
Related Articles
Development / 10 min read
Development / 10 min read
How to Change or Edit Your WordPress Footer
If you’re starting to dive deeper into your WordPress site, you may be wondering how to change or edit the contents of your footer. After all, it’s the part of…
Read MoreDevelopment / 8 min read
Development / 8 min read
How to Change the Size of Your Featured Image in WordPress
The size of the featured image in WordPress is an important factor in the look and feel of your posts, pages, and custom post types. As a result, you may…
Read MoreDevelopment / 7 min read
Development / 7 min read
How to Clean Up your WordPress Uploads Folder?
The WordPress Uploads folder is located on wp-content and contains all media files, such as images, videos, and documents, that you upload through the WordPress Media Library. Over time, it…
Read MoreDevelopment / 2 min read
Development / 2 min read
How to Change the Color of Social Media Icons in WordPress?
Looking for how to change the color of social media icons in WordPress? By default, social media icons in WordPress have unique colors. This can be great, but it can…
Read MoreDevelopment / 7 min read
Development / 7 min read
How to Hide the Page and Post Title in WordPress
Do you want to hide your post or page title in WordPress? Sometimes, it may be necessary to do it, especially if you want your home page or informational pages…
Read More