Bird
0
0

What is wrong with this code that tries to add a capability to the Author role?

medium📝 Debug Q7 of 15
Wordpress - WordPress Settings and Configuration
What is wrong with this code that tries to add a capability to the Author role?
$role = get_role('author');
$role->add_cap('edit_others_posts');
AMust call remove_cap() before add_cap()
BAuthor role cannot have new capabilities added
Cadd_cap() method does not exist on role objects
DNo error, code works correctly
Step-by-Step Solution
Solution:
  1. Step 1: Verify get_role() returns role object

    get_role() returns a WP_Role object with add_cap() method.
  2. Step 2: Confirm add_cap() usage

    add_cap() adds a capability to the role; this is valid for Author role.
  3. Final Answer:

    No error, code works correctly -> Option D
  4. Quick Check:

    add_cap() is valid method on role objects [OK]
Quick Trick: Use add_cap() to add capabilities to roles [OK]
Common Mistakes:
  • Thinking add_cap() doesn't exist
  • Believing roles are immutable
  • Confusing add_cap() with remove_cap()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes