WordPress Users and Roles
WordPress has a built in user system. Every person who can log in to your site is a user, and every user has a role. The role decides what the person is allowed to do. This is important for security: give people only the access they really need.
Default user roles
$ wp role list –fields=role,name
role name
administrator Administrator
editor Editor
author Author
contributor Contributor
subscriber Subscriber
| Role | What the user can do |
|---|---|
| Administrator | Everything: settings, themes, plugins, users and all content |
| Editor | Publish and manage all posts and pages, including the posts of others, and moderate comments |
| Author | Write, publish and edit his own posts, and upload files |
| Contributor | Write and edit his own posts, but cannot publish them, and cannot upload files |
| Subscriber | Only manage his own profile. Can read the content |
Capabilities
A role is only a named group of capabilities. A capability is one small permission, like publish_posts or manage_options. We can check what is allowed with a few lines of PHP:
$ wp user create anita anita@example.com –role=author –user_pass=Secret123! –porcelain
2
$ wp user create sam sam@example.com –role=contributor –user_pass=Secret123! –porcelain
3
$ wp user list –fields=ID,user_login,roles
ID user_login roles
1 admin administrator
2 anita author
3 sam contributor
$ wp eval 'foreach (["publish_posts","edit_others_posts","upload_files","manage_options"] as $c) { echo str_pad($c, 20), "anita: ", user_can(2, $c) ? "yes" : "no", " sam: ", user_can(3, $c) ? "yes" : "no", "\n"; }'
publish_posts anita: yes sam: no
edit_others_posts anita: no sam: no
upload_files anita: yes sam: no
manage_options anita: no sam: no
Manage users
- Users > Add User creates a user. Choose the lowest role which works.
- Use a strong password, and ask admins to turn on two factor login with a plugin.
- When a user leaves, delete the user and assign the content to someone else.
- Users > Profile is where every user edits his own name, password and options.
Create your own role
Plugins like User Role Editor help you to change capabilities. Developers can also do it with code, by using add_role():
add_role('shop_manager_lite', 'Shop Manager Lite', ['read' => true, 'edit_posts' => true]);
$role = get_role('shop_manager_lite');
echo $role->name . ': ' . implode(', ', array_keys($role->capabilities));
shop_manager_lite: read, edit_posts
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |