Day 4: Managing Users with WP-CLI

Creating, Updating, and Deleting Users

WP-CLI provides powerful commands for managing users on your WordPress site. Let’s explore how to create, update, and delete users using WP-CLI.

  • To create a new user:
wp user create username email@example.com --role=author --user_pass=password123

This command creates a new user with the specified username, email, role, and password. You can customize the role and other user metadata as needed.

  • To update an existing user:
wp user update 123 --display_name="New Display Name" --role=editor

This command updates the user with ID 123, changing their display name and role to editor. You can update various user fields using this command.

  • To delete a user:
wp user delete 123 --reassign=456

This command deletes the user with ID 123 and reassigns their content to the user with ID 456. If you don’t want to reassign content, you can omit the --reassign parameter.

Managing User Roles and Capabilities

WP-CLI allows you to manage user roles and their associated capabilities. Here are some examples:

  • To list all available roles:
wp role list
  • To create a new role:
wp role create custom_role "Custom Role"
  • To add a capability to a role:
wp role add custom_role edit_posts
  • To remove a capability from a role:
wp role remove custom_role edit_posts
  • To assign a role to a user:
wp user set-role 123 custom_role

Best Practices

When managing users with WP-CLI, consider the following best practices:

  • Use strong, unique passwords for each user account
  • Regularly review and update user roles and capabilities to ensure proper access control
  • Use the --porcelain flag when scripting user management tasks to ensure consistent output
  • Backup your user database before making bulk changes or deletions

In the next lesson, we’ll explore how to manage your WordPress database using WP-CLI. Stay tuned!