Bird
Raised Fist0
Wordpressframework~10 mins

User roles and permissions in Wordpress - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the current user's role in WordPress.

Wordpress
$user = wp_get_current_user();
$role = $user->[1][0];
Drag options to blanks, or click blank then click option'
Aroles
BID
Cuser_login
Duser_email
Attempts:
3 left
💡 Hint
Common Mistakes
Using user_login or user_email instead of roles
Trying to access roles as a method instead of a property
2fill in blank
medium

Complete the code to check if the current user has the 'editor' role.

Wordpress
if (in_array('[1]', wp_get_current_user()->roles)) {
    // User is an editor
}
Drag options to blanks, or click blank then click option'
Aeditor
Badministrator
Csubscriber
Dauthor
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'author' or 'subscriber' instead of 'editor'
Using equality operator instead of in_array for roles array
3fill in blank
hard

Fix the error in the code to add a custom capability 'edit_reports' to the 'editor' role.

Wordpress
$role = get_role('[1]');
$role->add_cap('edit_reports');
Drag options to blanks, or click blank then click option'
Asubscriber
Beditor
Ccontributor
Dauthor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'author' or 'subscriber' which do not have editing capabilities
Passing a display name instead of the role slug
4fill in blank
hard

Fill both blanks to remove the 'delete_posts' capability from the 'author' role.

Wordpress
$role = get_role('[1]');
$role->[2]('delete_posts');
Drag options to blanks, or click blank then click option'
Aauthor
Badd_cap
Cremove_cap
Dsubscriber
Attempts:
3 left
💡 Hint
Common Mistakes
Using add_cap instead of remove_cap
Trying to remove capability from 'subscriber' role
5fill in blank
hard

Fill all three blanks to create a new role 'manager' with 'read', 'edit_posts', and 'publish_posts' capabilities.

Wordpress
add_role('[1]', 'Manager', array(
    '[2]' => true,
    '[3]' => true,
    'publish_posts' => true
));
Drag options to blanks, or click blank then click option'
Amanager
Bread
Cedit_posts
Dsubscriber
Attempts:
3 left
💡 Hint
Common Mistakes
Using an existing role slug like 'subscriber'
Forgetting to set capabilities to true

Practice

(1/5)
1. What is the main purpose of user roles in WordPress?
easy
A. To group permissions and control what users can do
B. To change the website's theme
C. To add new plugins automatically
D. To backup the website data

Solution

  1. Step 1: Understand the concept of user roles

    User roles in WordPress are designed to group permissions for users.
  2. Step 2: Identify the purpose of roles

    Roles control what actions users are allowed to perform on the site.
  3. Final Answer:

    To group permissions and control what users can do -> Option A
  4. Quick Check:

    User roles = group permissions [OK]
Hint: Roles group permissions to control user actions [OK]
Common Mistakes:
  • Confusing roles with themes or plugins
  • Thinking roles backup data
  • Assuming roles add new features automatically
2. Which function is used to add a new user role in WordPress?
easy
A. add_user_role()
B. add_role()
C. create_role()
D. new_role()

Solution

  1. Step 1: Recall WordPress role functions

    The correct function to add a new role is add_role().
  2. Step 2: Verify function names

    Other options like add_user_role() or create_role() do not exist in WordPress core.
  3. Final Answer:

    add_role() -> Option B
  4. Quick Check:

    Adding roles = add_role() [OK]
Hint: Use add_role() to create new roles [OK]
Common Mistakes:
  • Using add_user_role() which is not a WordPress function
  • Confusing with create_role() or new_role()
  • Trying to add roles without this function
3. What will the following code output if the current user has the 'edit_posts' capability?
if (current_user_can('edit_posts')) {
  echo 'Can edit posts';
} else {
  echo 'Cannot edit posts';
}
medium
A. Cannot edit posts
B. Syntax error
C. No output
D. Can edit posts

Solution

  1. Step 1: Understand current_user_can() behavior

    This function checks if the current user has a specific capability.
  2. Step 2: Analyze the condition

    If the user has 'edit_posts', the code echoes 'Can edit posts'.
  3. Final Answer:

    Can edit posts -> Option D
  4. Quick Check:

    Has capability = prints confirmation [OK]
Hint: current_user_can() returns true if user has capability [OK]
Common Mistakes:
  • Assuming it returns false always
  • Confusing capability names
  • Expecting syntax errors from correct code
4. Identify the error in this code snippet for removing a user role:
remove_role('editor');
medium
A. remove_role() requires two parameters
B. remove_role() cannot remove default roles
C. No error, this code correctly removes the 'editor' role
D. The role name must be capitalized

Solution

  1. Step 1: Check remove_role() usage

    The function remove_role() takes one parameter: the role slug. This usage is correct.
  2. 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.
  3. Final Answer:

    No error, this code correctly removes the 'editor' role -> Option C
  4. Quick Check:

    remove_role() works on all roles [OK]
Hint: remove_role('editor') works fine [OK]
Common Mistakes:
  • Thinking remove_role needs two parameters
  • Believing default roles cannot be removed
  • Assuming role names must be capitalized
5. You want to create a custom role 'content_manager' that can edit posts and moderate comments. Which code snippet correctly adds this role with these capabilities?
hard
A. add_role('content_manager', 'Content Manager', ['edit_posts' => true, 'moderate_comments' => true]);
B. add_role('content_manager', 'Content Manager', ['edit_posts', 'moderate_comments']);
C. add_role('content_manager', 'Content Manager', ['edit_posts' => false, 'moderate_comments' => true]);
D. add_role('content_manager', 'Content Manager', ['edit_posts' => true, 'delete_posts' => true]);

Solution

  1. Step 1: Understand add_role() parameters

    The function takes role slug, display name, and an array of capabilities with boolean values.
  2. Step 2: Check capabilities array

    Capabilities must be keys with true/false values to grant or deny permissions.
  3. 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.
  4. Final Answer:

    add_role('content_manager', 'Content Manager', ['edit_posts' => true, 'moderate_comments' => true]); -> Option A
  5. Quick Check:

    Capabilities array with true values = correct role setup [OK]
Hint: Capabilities array needs keys with true/false values [OK]
Common Mistakes:
  • Passing capabilities as list without keys
  • Setting capability to false when it should be true
  • Adding wrong capabilities not requested