User roles and permissions help control what different users can do on a WordPress site. This keeps the site safe and organized.
User roles and permissions in Wordpress
<?php // Add a new role add_role('custom_role', 'Custom Role', [ 'read' => true, 'edit_posts' => true, 'delete_posts' => false ]); // Check if user has a capability if (current_user_can('edit_posts')) { // User can edit posts } // Remove a role remove_role('custom_role'); ?>
Roles group capabilities (permissions) for easier management.
Use current_user_can() to check permissions before actions.
<?php // Add an Editor role with extra capability add_role('custom_editor', 'Custom Editor', [ 'read' => true, 'edit_posts' => true, 'publish_posts' => true, 'moderate_comments' => true ]); ?>
<?php // Check if current user can delete posts if (current_user_can('delete_posts')) { echo 'You can delete posts.'; } else { echo 'You cannot delete posts.'; } ?>
<?php
// Remove a custom role
remove_role('custom_editor');
?>This code adds a new role with specific permissions, checks if the current user can publish posts, prints a message, and then removes the role.
<?php // Add a new role called 'content_manager' add_role('content_manager', 'Content Manager', [ 'read' => true, 'edit_posts' => true, 'publish_posts' => true, 'delete_posts' => false ]); // Check if current user can publish posts if (current_user_can('publish_posts')) { echo 'You can publish posts.'; } else { echo 'You cannot publish posts.'; } // Remove the role after use remove_role('content_manager'); ?>
Roles and permissions are stored in the database and persist until changed.
Be careful when removing roles; users assigned to that role lose those permissions.
Always check permissions before allowing sensitive actions to keep your site secure.
User roles group permissions to control what users can do.
Use add_role() to create roles and remove_role() to delete them.
Check permissions with current_user_can() before performing actions.
Practice
Solution
Step 1: Understand the concept of user roles
User roles in WordPress are designed to group permissions for users.Step 2: Identify the purpose of roles
Roles control what actions users are allowed to perform on the site.Final Answer:
To group permissions and control what users can do -> Option AQuick Check:
User roles = group permissions [OK]
- Confusing roles with themes or plugins
- Thinking roles backup data
- Assuming roles add new features automatically
Solution
Step 1: Recall WordPress role functions
The correct function to add a new role isadd_role().Step 2: Verify function names
Other options likeadd_user_role()orcreate_role()do not exist in WordPress core.Final Answer:
add_role() -> Option BQuick Check:
Adding roles = add_role() [OK]
- Using add_user_role() which is not a WordPress function
- Confusing with create_role() or new_role()
- Trying to add roles without this function
if (current_user_can('edit_posts')) {
echo 'Can edit posts';
} else {
echo 'Cannot edit posts';
}Solution
Step 1: Understand current_user_can() behavior
This function checks if the current user has a specific capability.Step 2: Analyze the condition
If the user has 'edit_posts', the code echoes 'Can edit posts'.Final Answer:
Can edit posts -> Option DQuick Check:
Has capability = prints confirmation [OK]
- Assuming it returns false always
- Confusing capability names
- Expecting syntax errors from correct code
remove_role('editor');Solution
Step 1: Check remove_role() usage
The function remove_role() takes one parameter: the role slug. This usage is correct.Step 2: Verify default roles behavior
WordPress allows removing default roles like 'editor' using remove_role(). The code executes without error, though default roles may be re-registered later.Final Answer:
No error, this code correctly removes the 'editor' role -> Option CQuick Check:
remove_role() works on all roles [OK]
- Thinking remove_role needs two parameters
- Believing default roles cannot be removed
- Assuming role names must be capitalized
Solution
Step 1: Understand add_role() parameters
The function takes role slug, display name, and an array of capabilities with boolean values.Step 2: Check capabilities array
Capabilities must be keys with true/false values to grant or deny permissions.Step 3: Match required capabilities
Only add_role('content_manager', 'Content Manager', ['edit_posts' => true, 'moderate_comments' => true]); correctly grants 'edit_posts' and 'moderate_comments' as true.Final Answer:
add_role('content_manager', 'Content Manager', ['edit_posts' => true, 'moderate_comments' => true]); -> Option AQuick Check:
Capabilities array with true values = correct role setup [OK]
- Passing capabilities as list without keys
- Setting capability to false when it should be true
- Adding wrong capabilities not requested
Ready to prove it?
Pass the test and this pattern moves unknown
