WPI-CLI is a very useful tool for managing your website built on WordPress from the Command Line Interface (CLI).
For example, if you want to create a custom role you can use the wp role command. Then, using the wp head command, you can add permissions to it. Once you have set permissions for the new role, you can assign this role to any WordPress user using the wp user command.
A practical example of customizing a user's role would be to give the webmaster limited access so that they can manage your comments, posts, and pages. However, they will not have access to changing the theme, plugins and users, due to the limited permissions granted by the role assigned to their user type.
To create a custom role, add capabilities to it, and assign a role to a WordPress user, using WP-CLI, we recommend that you follow the steps below:
Creating a Custom Role
In WordPress, a role is the set of configured functions that a user, with this assigned role, is allowed to perform. Here are the steps required to create a role using the WP-CLI command: wp role.
- Log in to the server via SSH with the cPanel user who owns the website you want to work on.
- Use the cd command to access the root folder of your website eg:
cd public_html/website
Replace public_html/website with the real path to the root folder of your website.
Execute the following command:
wp role create $role-key $role-name
Replace $role-key and $role-name with your own custom values. After running the command, a successful role creation message will appear, as in the output below:
[email protected] [~/public_html]# wp role create moderator Moderator Success: Role with key moderator created.
Now that you have created the custom role, you can move on to the next step, adding capabilities to it (permissions).
Adding capabilities to a Role
Permissions define the access functions with which a certain role is configured, when a user is assigned that role. By default, WordPress has roles and some functions assigned to those roles that fit a variety of user needs. However, if you want to create a custom role, you will need to specify the permissions that you give to the users to whom you assign that role. To add permissions to a custom role, follow the steps below, using the WP-CLI command: wp cap.
- Log in to the server via SSH with the cPanel user who owns the website you want to work on.
- Use the cd command to access the root folder of your website eg:
cd public_html/website
Replace public_html/website with the real path to the root folder of your website.
Execute the following command:
wp cap add $role $cap
Replace $roles and $head with the permissions (separated by a space) that you allow to the users to whom you will assign that role. After completing the command run you will receive a success message, similar to the following output:
[email protected] [~/public_html]# wp cap add ‘moderator’ read edit_posts publish_posts upload_files edit_published_posts unfiltered_html read_private_pages edit_private_pages read_private_posts edit_private_posts publish_pages edit_published_pages edit_others_pages edit_pages edit_others_posts manage_links manage_categories moderate_comments
Success: Added 18 capabilities to ‘moderator’ role.
Next, after adding custom role permissions, you can assign this role to a user.
Assigning a role to a user
When assigning a role to a user, limit their access to the permissions added to that role. You can assign a role to the user creation or you can update the role of an existing user using the WP-CLI wp user command.
- Assign a role when creating the user
- Log in to the server via SSH with the cPanel user who owns the website you want to work on.
- Use the cd command to access the root folder of your website eg:
cd public_html/website
Replace public_html/website with the real path to the root folder of your website.
Execute the following command:
wp user create –prompt
The table below contains examples of values to answer when prompted:
| Field | Description | Value |
|---|---|---|
| 1/10 <user-login>: | Enter the username the new user will login with. | wpjohn |
| 2/10 <user-email>: | Enter the email address to assign to the new user. | wpjohn@domain.com |
| 3/10 [–role=<role>]: | Enter the role to assign to the new user. | moderator |
| 4/10 [–user_pass=<password>]: | Enter the password the new user will login with. | #edme83me |
| 5/10 [–user_registered=<yyyy-mm-dd>]: | Enter the date to set for the new user’s date of registration. | 2222-12-18 |
| 6/10 [–display_name=<name>]: | Enter the new user’s name, as it should appear on the website. | WP Admin |
| 7/10 [–first_name=<first_name>]: | Enter the new user’s first name. | WP |
| 8/10 [–last_name=<last_name>]: | Enter the new user’s last name. | Admin |
| 9/10 [–send-email] (Y/n): | Enter Y to send the new user details of the account via email or enter n to skip. | n |
| 10/10 [–porcelain] (Y/n): | Enter Y to output just the new user id or enter n to skip | n |
After you have provided all the information, the user will be created and you will see a success message, as in the example below:
[email protected] [~/public_html]# wp user create --prompt
1/10 <user-login>: wpadmin
2/10 <user-email>: [email protected]
3/10 [--role=<role>]: moderator
4/10 [--user_pass=<password>]: @DMIN-p@s$w0rd123!
5/10 [--user_registered=<yyyy-mm-dd>]: 2017-12-18
6/10 [--display_name=<name>]: WP Admin
7/10 [--first_name=<first_name>]: WP
8/10 [--last_name=<last_name>]: Admin
9/10 [--send-email] (Y/n): n
10/10 [--porcelain] (Y/n): n
Success: Created user 3.
- Updating the role of an existing user
- Log in to the server via SSH with the cPanel user who owns the website you want to work on.
- Use the cd command to access the root folder of your website eg:
cd public_html/website
Replace public_html/my_website with the real path to the root folder of your website.
Execute the following command:
wp user set-role $user $role
Replace $user and $roles with your real user and custom role. You will see a success message, as in the example below:
[email protected] [~/public_html]# wp user set-role wpadmin moderator
Success: Added wpadmin (3) to https://domeniu.ro as moderator.
For assistance in applying the above instructions on HostX servers please contact us. HostX offers the fastest WordPress hosting and can help you solve your problems.
