Managing users on a WordPress site can become time-consuming, especially as your website scales. The WordPress Command Line Interface (WP-CLI) offers a powerful way to streamline these tasks directly from your terminal. Whether you want to add, update, or delete users, WP-CLI makes it fast, efficient, and error-free.
Being a developer with over a decade of experience, I would highly recommend all the developers to start using WP CLI on daily basis to save time and efforts.
In this guide, we’ll learn how to manage users using WP-CLI, covering everything from user creation to role assignments and deletion.
What is WP CLI?
WP-CLI is a command-line tool for managing WordPress sites, designed to save time on repetitive tasks. Instead of navigating through the WordPress dashboard, you can use WP-CLI to perform tasks like updating plugins, configuring themes, managing posts, and, as we’ll explore here, managing users—all from your terminal.
For developers and site administrators, WP-CLI is a game changer, offering faster workflows and automation potential.
To get started, make sure WP-CLI is installed on your server. If not, visit the WP-CLI website for installation instructions.
Why Use WP-CLI for User Management?
Managing WordPress users through the admin dashboard is intuitive but can be slow when handling many users. With WP-CLI, you can manage users much faster with commands that let you perform bulk actions or automate routine tasks. Here are some benefits:
- Speed: Instantly add, update, or remove users without waiting for page loads.
- Efficiency: Manage roles and permissions easily without clicking through multiple screens.
- Automation: Set up scripts to handle routine user management tasks, like removing inactive users or assigning roles.
- Bulk Actions: Perform actions on multiple users at once, such as batch updating roles or removing spammers.
WP-CLI Commands for Managing Users
Now, let’s explore the key commands that you can use to manage users with WP-CLI.
Adding a New User
To create a new user, WP-CLI provides a simple command that can be run in your terminal. Here’s how to create a user named “John Doe” with the username “johndoe” and a password.
wp user create johndoe johndoe@example.com --role=editor --user_pass=strongpassword
- Username:
johndoe
- Email:
johndoe@example.com
- Role:
editor
(This can be any default or custom role likeadministrator
,author
, etc.) - Password:
strongpassword
(Use Strong Password generator tools to generate one)
This command creates a user, assigns them the Editor role, and sets the password. You can customize the role as needed.
Update User Roles and Information
To update a user’s role or change other information, use the wp user update
command.
Example – Changing User Role to Administrator:
wp user update johndoe --role=administrator
You can also update a user’s email, password, or other profile information.
Example – Changing Email:
wp user update johndoe --user_email=newemail@example.com
Example – Resetting a Password:
wp user update johndoe --user_pass=newpassword123
WP-CLI allows you to update multiple users at once, which is useful for batch role updates or modifying profile information across the site.
Deleting a User
Deleting users via WP-CLI is fast and efficient. Use the following command to delete a user:
Example – Delete User:
wp user delete johndoe
You can also delete a user and reassign their posts to another user:
wp user delete johndoe --reassign=<another_user_id>
This command is useful when deleting an admin or editor and transferring content ownership.
Listing Users
If you want to see all the users on your site, use the following command:
wp user list
This will display user IDs, usernames, roles, and emails in a table.
You can also filter the results by role:
wp user list --role=editor
Fetching User Information
To retrieve details about a specific user, such as their email, role, or any custom metadata, use the following command:
wp user get <user> --fields=user_login,user_email,display_name,roles
For example, to get details of a user named johndoe
:
wp user get johndoe --fields=user_login,user_email,display_name,roles
You can also include custom fields by specifying them with the --fields
option. This will return the requested user data in a clean table format.
Advanced Bulk User Creation with WP-CLI
In advanced scenarios, you might want to bulk create users with detailed metadata, such as roles, passwords, and custom user fields, using WP-CLI. Here’s how to do it using a CSV file and a Bash script.
1. Prepare the CSV File
Create a CSV file (users.csv
) with the following structure:
username,email,password,role,meta_key_1,meta_key_2
johndoe,johndoe@example.com,password123,editor,custom_field1_value,custom_field2_value
janedoe,janedoe@example.com,password123,subscriber,custom_field1_value,custom_field2_value
2. Create Users with Metadata
Use the following Bash script to loop through the CSV and create users:
while IFS=',' read -r username email password role meta1 meta2
do
wp user create "$username" "$email" --role="$role" --user_pass="$password"
wp user meta update "$username" meta_key_1 "$meta1"
wp user meta update "$username" meta_key_2 "$meta2"
done < users.csv
This script:
- Creates users with a username, email, password, and role.
- Adds custom metadata fields to each user (
meta_key_1
,meta_key_2
).
Explanation:
wp user create
: This command creates the user with the specified username, email, password, and role.wp user meta update
: This command adds custom metadata to the created user.
With this technique, you can efficiently create users and assign multiple attributes in bulk. This can be useful for onboarding teams or managing large membership sites.
Bonus Tips
- Backup Your Database: Before performing any user-related operations, create a backup of your WordPress database to prevent accidental data loss.
- Use Role Assignments Wisely: Assign appropriate roles to users based on their responsibilities to maintain security and manage access effectively.
- Regularly Review User Accounts: Periodically review user accounts to remove inactive or unnecessary accounts, enhancing site security and performance.
Frequently Asked Questions
Can I manage user roles through WP-CLI?
Yes, WP-CLI allows you to assign, update, and even create or delete roles through simple commands.
Is it possible to create multiple users at once with WP-CLI?
Yes, you can bulk create users using a CSV file and a simple bash script, making it easy to onboard multiple users.
What should I do if I accidentally delete a user?
If a user is deleted, their posts can be reassigned using the --reassign
option to avoid losing content.
Can I automate user management with WP-CLI?
Absolutely. WP-CLI works seamlessly in automated scripts for tasks like user creation, role assignment, and deletion, making it perfect for scaling operations.
Is WP-CLI available on all WordPress hosting platforms?
Not all hosting providers have WP-CLI pre-installed, but you can check with your hosting provider or install it manually on your server.
Conclusion
Managing users via WP-CLI is a powerful tool in any developer’s skillset or administrator’s toolkit. It simplifies user management, boosts productivity, and provides a way to automate workflows. Whether you’re creating users, resetting passwords, or assigning roles, WP-CLI gets the job done fast. For anyone managing a large WordPress site, knowing these commands will save hours of effort.
Loved this article? Feel free to share with your friends and colleagues using Twitter or any of your preferred social media.