0
0
Wordpressframework~10 mins

User roles and permissions in Wordpress - Interactive Code Practice

Choose your learning style9 modes available
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