0
0
Wordpressframework~10 mins

User roles and permissions in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - User roles and permissions
Define Roles
Assign Capabilities
Create Users
Assign Roles to Users
User Logs In
Check User Role & Permissions
Allow or Deny Actions
This flow shows how WordPress defines roles, assigns capabilities, creates users, and checks permissions when users perform actions.
Execution Sample
Wordpress
<?php
// Add a custom role
add_role('custom_editor', 'Custom Editor', ['edit_posts' => true, 'delete_posts' => false]);

// Assign role to user
$user = new WP_User($user_id);
$user->set_role('custom_editor');

// Check capability
if(current_user_can('edit_posts')) {
  // Allow editing
}
This code adds a custom role, assigns it to a user, and checks if the user can edit posts.
Execution Table
StepActionRole/Capability StateUser RolePermission CheckResult
1Define 'custom_editor' role with 'edit_posts' capabilityRoles: custom_editor(edit_posts: true, delete_posts: false)NoneN/ARole created
2Create user object for user_idRoles unchangedNoneN/AUser object ready
3Assign 'custom_editor' role to userRoles unchangedcustom_editorN/AUser role set
4User tries to edit postRoles unchangedcustom_editorCheck 'edit_posts'Allowed (true)
5User tries to delete postRoles unchangedcustom_editorCheck 'delete_posts'Denied (false)
6User tries to publish post (not assigned)Roles unchangedcustom_editorCheck 'publish_posts'Denied (false)
💡 Permissions checked for user actions; allowed or denied based on role capabilities.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5Final
RolesNone{custom_editor: {edit_posts: true, delete_posts: false}}{custom_editor: {edit_posts: true, delete_posts: false}}{custom_editor: {edit_posts: true, delete_posts: false}}{custom_editor: {edit_posts: true, delete_posts: false}}{custom_editor: {edit_posts: true, delete_posts: false}}
User RoleNoneNonecustom_editorcustom_editorcustom_editorcustom_editor
Permission CheckN/AN/AN/Aedit_posts: truedelete_posts: falsepublish_posts: false
Key Moments - 3 Insights
Why can the user edit posts but not delete them?
Because the 'custom_editor' role has 'edit_posts' set to true but 'delete_posts' set to false, as shown in execution_table rows 4 and 5.
What happens if a capability is not assigned to a role?
The capability defaults to false, so the user cannot perform that action, as seen in step 6 where 'publish_posts' is not assigned.
Does assigning a role to a user change the role's capabilities?
No, assigning a role to a user only links the user to that role; the role's capabilities remain unchanged (see variable_tracker for Roles).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What permission does the user have?
ACan edit posts
BCan delete posts
CCan publish posts
DNo permissions
💡 Hint
Check the 'Permission Check' and 'Result' columns at step 4 in execution_table.
At which step is the user assigned the 'custom_editor' role?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'User Role' column in execution_table to see when it changes from None.
If 'delete_posts' was set to true in the role, what would change in the execution_table?
AStep 4 result would be Denied
BStep 5 result would be Allowed
CUser role would change
DNo change
💡 Hint
Compare the 'Permission Check' and 'Result' columns for step 5.
Concept Snapshot
WordPress User Roles & Permissions:
- Roles group capabilities (actions users can do).
- Assign roles to users to grant permissions.
- Use add_role() to create roles with capabilities.
- Use current_user_can() to check permissions.
- Unassigned capabilities default to denied.
Full Transcript
In WordPress, user roles define what actions users can perform by grouping capabilities. First, roles are created with specific capabilities like editing or deleting posts. Then, users are assigned these roles. When a user tries to do something, WordPress checks if their role has the needed capability. If yes, the action is allowed; if not, it is denied. This system helps control access easily by managing roles instead of individual permissions for each user.