Bird
Raised Fist0
Wordpressframework~10 mins

User capability checks in Wordpress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the current user can edit posts.

Wordpress
<?php if (current_user_can([1])) { echo 'User can edit posts'; } ?>
Drag options to blanks, or click blank then click option'
A'edit_posts'
B'manage_options'
C'publish_posts'
D'delete_posts'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'manage_options' which is for admin settings.
Using 'publish_posts' which checks publishing rights, not editing.
2fill in blank
medium

Complete the code to check if the current user can delete pages.

Wordpress
<?php if (current_user_can([1])) { echo 'User can delete pages'; } ?>
Drag options to blanks, or click blank then click option'
A'edit_pages'
B'publish_pages'
C'delete_pages'
D'read_pages'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'edit_pages' which checks editing, not deleting.
Using 'publish_pages' which checks publishing rights.
3fill in blank
hard

Fix the error in the code to correctly check if the user can manage options.

Wordpress
<?php if (current_user_can([1])) { echo 'User can manage options'; } ?>
Drag options to blanks, or click blank then click option'
A'manage_options'
B'manage_option'
C'manage_optionss'
D'manage'
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the capability string.
Using singular form instead of plural.
4fill in blank
hard

Fill both blanks to check if the user can edit a specific post with ID 42.

Wordpress
<?php if (current_user_can([1], [2])) { echo 'User can edit this post'; } ?>
Drag options to blanks, or click blank then click option'
A'edit_post'
B42
C'edit_posts'
D24
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'edit_posts' which checks general post editing, not a specific post.
Using wrong post ID.
5fill in blank
hard

Fill all three blanks to check if the user can publish a post of type 'book'.

Wordpress
<?php if (current_user_can([1], [2], [3])) { echo 'User can publish book'; } ?>
Drag options to blanks, or click blank then click option'
A'publish_post'
B'book'
C0
D'publish_books'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'publish_post' which is singular and generic.
Using wrong post type string.
Omitting the third argument.

Practice

(1/5)
1. What is the main purpose of the current_user_can() function in WordPress?
easy
A. To check if the current user has a specific capability
B. To create a new user role
C. To delete a user from the database
D. To update the user's profile information

Solution

  1. Step 1: Understand the function's role

    current_user_can() is designed to check permissions for the current user.
  2. Step 2: Identify correct usage

    It returns true if the user has the capability passed as an argument, otherwise false.
  3. Final Answer:

    To check if the current user has a specific capability -> Option A
  4. Quick Check:

    Permission check = A [OK]
Hint: Remember: current_user_can checks permissions, not user creation [OK]
Common Mistakes:
  • Confusing capability check with user creation
  • Thinking it modifies user data
  • Assuming it deletes users
2. Which of the following is the correct syntax to check if the current user can edit posts?
easy
A. current_user_can(edit_posts);
B. current_user_can['edit_posts'];
C. current_user_can('edit_posts');
D. current_user_can->edit_posts();

Solution

  1. Step 1: Identify correct function call syntax

    WordPress functions use parentheses and string arguments in quotes.
  2. Step 2: Check each option

    Only current_user_can('edit_posts'); uses parentheses and quotes correctly.
  3. Final Answer:

    current_user_can('edit_posts'); -> Option C
  4. Quick Check:

    Function call with string argument = B [OK]
Hint: Use parentheses and quotes for capability strings [OK]
Common Mistakes:
  • Omitting quotes around capability
  • Using square brackets instead of parentheses
  • Using arrow notation incorrectly
3. What will the following code output if the current user cannot delete posts?
if (current_user_can('delete_posts')) {
  echo 'Can delete posts';
} else {
  echo 'Cannot delete posts';
}
medium
A. Can delete posts
B. No output
C. Error: function not found
D. Cannot delete posts

Solution

  1. Step 1: Understand the condition

    The if checks if the user has 'delete_posts' capability.
  2. Step 2: Evaluate the condition result

    If the user cannot delete posts, the condition is false, so the else block runs.
  3. Final Answer:

    Cannot delete posts -> Option D
  4. Quick Check:

    False condition triggers else = D [OK]
Hint: If user lacks capability, else block runs [OK]
Common Mistakes:
  • Assuming output is always 'Can delete posts'
  • Confusing capability names
  • Expecting errors if capability missing
4. Identify the error in this code snippet:
if (current_user_can('edit_post')) {
  echo 'User can edit post';
}
medium
A. Missing semicolon after echo statement
B. The capability name should be 'edit_posts' not 'edit_post'
C. current_user_can() requires two arguments
D. The function should be called as currentUserCan()

Solution

  1. Step 1: Check capability name correctness

    WordPress uses 'edit_posts' (plural) as the capability, not 'edit_post'.
  2. Step 2: Verify syntax and function usage

    Semicolon is present, function takes one argument, and function name is correct.
  3. Final Answer:

    The capability name should be 'edit_posts' not 'edit_post' -> Option B
  4. Quick Check:

    Correct capability string = A [OK]
Hint: Check exact capability names in WordPress docs [OK]
Common Mistakes:
  • Using singular instead of plural capability names
  • Assuming function needs multiple arguments
  • Incorrect function name casing
5. You want to show a special admin menu only to users who can manage options and edit themes. Which code correctly checks both capabilities before showing the menu?
hard
A. if (current_user_can('manage_options') && current_user_can('edit_themes')) { show_menu(); }
B. if (current_user_can('manage_options' || 'edit_themes')) { show_menu(); }
C. if (current_user_can(['manage_options', 'edit_themes'])) { show_menu(); }
D. if (current_user_can('manage_options') || current_user_can('edit_themes')) { show_menu(); }

Solution

  1. Step 1: Understand logical AND for both capabilities

    To require both permissions, use && between two separate current_user_can() calls.
  2. Step 2: Evaluate each option's logic

    if (current_user_can('manage_options') && current_user_can('edit_themes')) { show_menu(); } correctly uses && with two calls; A and B misuse syntax; D uses || which allows either permission.
  3. Final Answer:

    if (current_user_can('manage_options') && current_user_can('edit_themes')) { show_menu(); } -> Option A
  4. Quick Check:

    Both capabilities require && = C [OK]
Hint: Use && with separate calls to check multiple capabilities [OK]
Common Mistakes:
  • Using || instead of && when both permissions are needed
  • Passing multiple capabilities as one argument
  • Using incorrect syntax for logical operators
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard