Managing Users and Roles Using WordPress CLI

Here’s how you can manage users' roles using WordPress CLI - With commands listing.

Cloudways Product avatar
Written by Cloudways Product
Updated over a week ago

This KB article will teach you how you can manage your users using WordPress Command Line Interface (CLI). Managing users using WP-CLI is super easy and handy, and here comes the best part that you do not need a web browser to operate WP-CLI.

WP-CLI is pre-installed on all the servers deployed on Cloudways, and below mentioned steps would demonstrate how you can operate WP-CLI and manage users.

What is WordPress CLI?

WP-CLI is a command-line interface for WordPress, and it is a set of command-line tools for managing WordPress installations on a server. It allows the WordPress admin users to carry out different administrative tasks without actually using a web browser. This management through the command-line interface allows the users to manage their WordPress websites by executing a set of defined commands. The scope of the tasks mainly includes upgrades, taking database backups, publishing new posts, and much more.

What are WordPress User Roles?

In WordPress, a User Role is a combination of:

  1. Role

  2. Capabilities

A role is the name of a user group that will be displayed in your WordPress Admin Panel, and capabilities are the privileges that admins can enable or disable.

By default, WordPress has six basic user roles.

  1. Super Admin: The profile that has access to the entire website, including network administrative features.

  2. Administrator: The profile(s) that has all administrative privileges.

  3. Editor: The profile(s) that can create, edit, publish theirs, and other users’ posts.

  4. Author: The profile(s) that can create, edit, publish their posts only.

  5. Contributor: The profile(s) that can create, edit their posts but cannot publish them.

  6. Subscriber: The profile(s) that can only manage their profiles.

Why using WordPress Users and Roles?

If you run your WordPress Application single-handedly then you might not have thought of WordPress’ Users and Roles but if you have a team who also look after your WordPress site or you want to give specific responsibilities to someone then you might need to consider reading this further as WordPress Users and Roles feature will enable you to decide the domain of various users and restrict their access to their needs. For example, the ability to post, install or uninstall any plugin.

How to Manage Users and Roles Using WordPress CLI

Here are a few simple steps to help you get started with managing users and roles using WordPress CLI.

Step# 1

First of all, you need to connect to your server remotely via SSH so you can use WP-CLI.

If you would like to know what is SSH and why it is used, then Click Here to find out. In this example, we are using Master Credentials to access the server remotely.

You can connect to your server via SSH in two ways, so choose your preferred option from the below options. You can also click on the hyperlink text of bullet points to learn about connecting remotely to the server.

In this example, we have used an SSH client named PuTTY. You will see a similar window after successful connection as shown below:

Step# 2

Now, you need to go to that specific directory where your webroot is located. In other words, where your wp-config.php file is located else, WP-CLI will not work. By default, it is in the public_html directory and we will also follow the default path to operate WP-CLI in this example. Enter a command mentioned below to go into the public_html folder.

cd applications/<your_application_name>/public_html/

Important

Angle brackets are included to indicate the positions of your inputs so make sure to remove the angle brackets. Your application name is the same as your Database name (DB name). Click Here to find out where your application name is located.

You will see a similar window after running the above command successfully.

Step# 3

You may start using WP-CLI once you are in the public_html directory and here we have listed a few useful operations and commands.

Creating New User

  • Writing the following command will create a new user with the defined user_login (username), email address, role, and password will be auto-generated.

wp user create <user_login> <user-email> --role=<role>

For example: wp user create john [email protected] --role=author

  • Similarly, you can also append a few other commands with a username and email as well depending on your requirements when creating a new user.

For instance, if you want to create a user with a defined username, email address, role, and custom password, then the command will be as follows:

wp user create <user_login> <user-email> --role=<role> --user_pass=<password>

For example: wp user create marie [email protected] --role=contributor --user_pass=ejguykk@44ap

Command

Description

--role=<role>

The role of the user to create. e.g. include ‘administrator’, ‘editor’, ‘author’, ‘contributor’, ‘subscriber’. Default: default role.

--user_pass=<password>

Creating custom password. Default: auto-generated.

--user_registered=<yyyy-mm-dd-hh-ii-ss>

Date when the user registered. Default: current date.

--display_name=<name>

Display name.

--user_nicename=<nice_name>

A string that contains a URL-friendly name for the user. The default is the user’s username.

--user_url=<url>

A string containing the user’s URL for the user’s website.

--nickname=<nickname>

The user’s nickname, defaults is the user’s username.

--first_name=<first_name>

User’s first name.

--last_name=<last_name>

The user’s last name.

--description=<description>

A string containing content about the user.

--rich_editing=<rich_editing>

A string for whether to enable the rich editor or not. False if not empty.

--send-email

Send an email to the user with their new account details.

--porcelain

Output just the new user id.

Listing Users

  • The listing users command will help you to view all the users of your WordPress application. Following is the command.

wp user list

  • Likewise, you may append a few more parameters with the command mentioned above as per your needs.

For instance, if you would like to view the ID, and display the names of all the users in JSON format, then you may type this command.

wp user list --fields=ID,display_name --format=json

Command

Description

--role=<role>

Only display users with a certain role. e.g. include ‘administrator’, ‘editor’, ‘author’, ‘contributor’, ‘subscriber’.

--<field>=<value>

Control output by one or more arguments of WP_User_Query().

--network

List all users in the network for multisite.

--field=<field>

Prints the value of a single field for each user.

--fields=<fields>

Limit the output to specific object fields.

These fields will be displayed by default for each user:

  • ID

  • user_login

  • display_name

  • user_email

  • user_registered

  • roles

These fields are optionally available:

  • user_pass

  • user_nicename

  • user_url

  • user_activation_key

  • user_status

  • spam

  • deleted

  • caps

  • cap_key

  • allcaps

  • filter

  • url

--format=<format>

Render output in a particular format.

  • default: table

Options:

  • table

  • csv

  • ids

  • json

  • count

  • yaml

Updating User

  • You may type these commands if you would like to update an existing user.

For instance, if you would like to update display_name, and user_pass(password), then the following command will be helpful.

wp user update <ID> --display_name=<display_name> --user_pass=<password>

For example: wp user update 2 --display_name=Johnny --user_pass!$j19087$$11

Command

Description

<user>…

The user login, user email, or user ID of the user(s) to update.

--user_pass=<password>

A string that contains the plain text password for the user.

--user_nicename=<nice_name>

A string that contains a URL-friendly name for the user. The default is the user’s username.

--user_url=<url>

A string containing the user’s URL for the user’s website.

--user_email=<email>

A string containing the user’s email address.

--display_name=<display_name>

A string that will be shown on the site. Defaults to user’s username.

--role=<role>

The user’s nickname, defaults to the user’s username.

--first_name=<first_name>

The user’s first name.

--last_name=<last_name>

The user’s last name.

--description=<description>

A string containing content about the user.

--rich_editing=<rich_editing>

A string for whether to enable the rich editor or not. False if not empty.

--user_registered=<yyyy-mm-dd-hh-ii-ss>

Date when the user registered.

--role=<role>

A string used to set the user’s role.

--<field>=<value>

One or more fields to update. For accepted fields, see wp_update_user().

--skip-email

Don’t send an email notification to the user.

Deleting User

  • You need to follow the below-mentioned command if you would like to delete the user.

For example, if you would like to delete the user and reassign that user’s posts to any other user:

wp user delete <user-id> --reassign=<user-id>

For example: wp user delete 3 --reassign=1

  • Likewise, you can also append a few other commands as well, depending on your requirements when deleting the user(s).

For example, if you would like to delete all users from any particular role and reassign their posts to any user

wp user delete $(wp user list --role=<user-role> --field=ID) --reassign=<user-id>

For example: wp user delete $(wp user list --role=author> --field=ID) --reassign=1

Important

If you do not reassign the posts, then all the posts of deleted users will also be deleted.

Command

Description

<user>…

The user login, user email, or user ID of the user(s) to delete.

--network

On multisite, delete the user from the entire network.

--reassign=<user-id>

User ID to reassign the posts to.

--yes

Answer yes to any confirmation prompts.

You can also manage the same using your WordPress Admin Panel as well so Click Here to learn how.

That’s it! We hope this article was helpful. If you need any help, then feel free to search your query on Cloudways Support Center or contact us via chat (Need a Hand > Send us a Message). Alternatively, you can also create a support ticket.

Did this answer your question?