Roles in WordPress
Let's say that you wanna build a website that'll allow some of your visitors to view certain information that others can't and even give certain visitors the ability to edit their own stuff. WordPress has inbuilt users!
Users in WordPress
You can create “users” on your WordPress site, and users can edit their own details on their profile page, that looks something like this…
Roles in WordPress
Then you can assign “roles” to each users, and here’s how they vary…
-
Subscriber
Can edit their profile. That’s it.
-
Contributor
Can edit their profile and write posts (They can’t publish posts, they get sent for review, and then someone with a higher role publishes it).
-
Author
Can edit their profile, write and publish their own posts.
-
Editor
Can edit their profile, write and publish posts their own posts and the posts of other users.
-
Administrator
Can do pretty much everything, most importantly they have control of everything beyound profile and post content. They can control the settings, plugins, themes and custom fields.
-
Creating Custom roles
You can create custom roles,
$role = "wholesale_customer"; $display_name = "Wholesale Customer"; $capabilities = array( 'read' => true, 'edit_posts' => false, 'edit_pages' => false, 'edit_others_posts' => false, 'create_posts' => false, 'manage_categories' => false, 'publish_posts' => false, 'edit_themes' => false, 'install_plugins' => false, 'update_plugin' => false, 'update_core' => false, 'read_private_pages' => true, 'read_private_posts' => true ); add_role( $role, $display_name, $capabilities);
and you can control what roles can see what posts…
$user = wp_get_current_user(); $allowed_roles = array('editor', 'administrator', 'wholesale_customer'); if( array_intersect($allowed_roles, $user->roles ) ) { //stuff here for editor, administrator and whole sale customers only }
Last modified: July 8, 2018
Mark Endley