How to create a Super Admin and assign it to all sites of a multisite using the WP-CLI

Two weeks ago, I’ve started to work on a new project. The whole WordPress website was versioned in Git, including a DDEV configuration and an SQL dump for local development. This was great, as it allowed me to get started really quickly. But after installing the project, I couldn’t find the login credentials.

Getting all users of the installation

The usual “admin” or “wordpress” users didn’t work, so I first checked the list of available users with the WP-CLI:

$ wp user list
+----+------------+--------------+------------------------+---------------------+---------------+
| ID | user_login | display_name | user_email             | user_registered     | roles         |
+----+------------+--------------+------------------------+---------------------+---------------+
| 11 | jo         | jo           | [email protected]         | 2023-06-01 01:30:30 | administrator |
| 22 | jane       | jane         | [email protected]       | 2023-06-02 02:30:30 | administrator |
| 33 | john       | john         | [email protected]       | 2023-06-03 03:30:30 | administrator |
+----+------------+--------------+------------------------+---------------------+---------------+

For none of these users (names have been changed) I could find any login credentials. Therefore, I had to create myself a new user.

Create an administrator user

I usually use the --prompt parameter when creating a new user (or other entities), so I don’t miss an important parameter:

$ wp user create --prompt
1/15 <user-login>: wapuu
2/15 <user-email>: [email protected]
3/15 [--role=<role>]: administrator
4/15 [--user_pass=<password>]: wordpress
5/15 [--user_registered=<yyyy-mm-dd-hh-ii-ss>]: 
6/15 [--display_name=<name>]: 
7/15 [--user_nicename=<nice_name>]: 
8/15 [--user_url=<url>]: 
9/15 [--nickname=<nickname>]: 
10/15 [--first_name=<first_name>]: 
11/15 [--last_name=<last_name>]: 
12/15 [--description=<description>]: 
13/15 [--rich_editing=<rich_editing>]: 
14/15 [--send-email] (Y/n): n
15/15 [--porcelain] (Y/n): 
wp user create --role='administrator' --user_pass='wordpress'
Success: Created user 44.

Now I was able to log in to the site! But as soon as I was logged in, I’ve recognized that this was a multisite installation. So my new shiny administrator user was not able to see all the other sites.

Promote a user to a Super Admin

Using the WP-CLI you need a single command to promote a user to a Super Admin:

$ wp super-admin add 44
Success: Granted super-admin capabilities to 1 user.

After running this command, I was able to see all sites listed in the “Network Admin”. I would now be able to work with all of them. But since my new account was not directly associated with any of those sites, they would not show up in the “My Sites” dropdown in the adminbar.

Adding a user to all sites as an administrator

This specific multisite had only three sites, so adding my new user as an administrator to all of them is a rather quick task. But what about a network with 100 sites? That would take quite long and you might miss one site. Fortunately, you can use another WP-CLI command to solve this.

Getting a list of all sites

We would first need to get a list of all sites. This can be done with the following command:

$ wp site list
+---------+---------------------------+---------------------+---------------------+
| blog_id | url                       | last_updated        | registered          |
+---------+---------------------------+---------------------+---------------------+
| 1       | https://example.com/      | 2023-06-01 01:30:30 | 2023-06-01 01:30:30 |
| 2       | https://example.com/de/   | 2023-06-02 02:30:30 | 2023-06-02 02:30:30 |
| 3       | https://example.com/en/   | 2023-06-03 03:30:30 | 2023-06-03 03:30:30 |
+---------+---------------------------+---------------------+---------------------+

To assign the new user to one of these pages with a specific role, we can use this command:

$ wp user set-role 44 administrator --url=https://example.com/de/
Success: Added wapuu (44) to https://example.com/de as administrator.

We would now need to do the same things for every single site, using the url field. But for many sites, this also takes some time and we might make a mistake. So instead, I was looking for a single line command.

Adding the user as an administrator to all sites

I’m not the best shell developer, but with some trial and error, I came up with this command:

$ wp site list --field=url | xargs -I {} wp user set-role wapuu administrator --url={}
Success: Added wapuu (44) to https://example.com as administrator.
Success: Added wapuu (44) to https://example.com/de as administrator.
Success: Added wapuu (44) to https://example.com/en as administrator.

This command is getting the list of sites, as shown before, and for each of them only the url field. This is then passed with xargs to the second command, that would add the user to the site. Make sure to set the correct username (wapuu in this example) in the second command.

Conclusion

With some WP-CLI magic, you can quickly create an administration user, grant Super Admin permissions and associate it with every site in a multisite. I hope you’ll find this useful in one of your (future) multisite projects as well.

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

Your email address will not be published. Required fields are marked *