0
0
Wordpressframework~20 mins

User roles and permissions in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WordPress Roles & Permissions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a user with the 'Editor' role tries to delete a published post?

In WordPress, users have different roles with specific capabilities. Consider a user assigned the 'Editor' role. What will happen if this user attempts to delete a published post?

AThe user cannot delete any posts at all.
BThe user cannot delete published posts, only drafts or their own posts.
CThe user can delete the published post without restrictions.
DThe user can delete only posts they authored, not posts by others.
Attempts:
2 left
💡 Hint

Think about the default capabilities assigned to the Editor role in WordPress.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly adds a custom capability to the 'Author' role?

You want to add a custom capability named 'edit_special_content' to the 'Author' role in WordPress. Which code snippet does this correctly?

A$role = get_role('author'); $role->add_cap('edit_special_content');
B$role = get_role('Author'); $role->add_cap('edit_special_content');
Cget_role('Author')->add_cap('edit_special_content');
Dadd_cap('Author', 'edit_special_content');
Attempts:
2 left
💡 Hint

Remember that role slugs are lowercase and you need to get the role object before adding capabilities.

state_output
advanced
2:00remaining
What is the output of this code when checking if a user can 'publish_posts'?

Consider the following code snippet executed in a WordPress environment where the current user has the 'Contributor' role:

if (current_user_can('publish_posts')) {
  echo 'Can publish';
} else {
  echo 'Cannot publish';
}

What will be printed?

Wordpress
if (current_user_can('publish_posts')) {
  echo 'Can publish';
} else {
  echo 'Cannot publish';
}
ACannot publish
BCan publish
CFatal error: function current_user_can() not found
DNo output
Attempts:
2 left
💡 Hint

Check the default capabilities of the 'Contributor' role regarding publishing posts.

🔧 Debug
advanced
2:00remaining
Why does this code fail to add a capability to a custom role?

Given this code snippet, why does the custom capability not get added to the 'custom_role'?

add_action('init', function() {
  $role = get_role('custom_role');
  $role->add_cap('manage_special_feature');
});
Wordpress
add_action('init', function() {
  $role = get_role('custom_role');
  $role->add_cap('manage_special_feature');
});
AThe capability name 'manage_special_feature' is reserved and cannot be added.
BThe 'init' hook runs too early before roles are registered.
CThe 'custom_role' does not exist, so get_role returns null causing an error.
Dadd_cap() requires a second parameter to enable the capability.
Attempts:
2 left
💡 Hint

Check if the role 'custom_role' exists before adding capabilities.

🧠 Conceptual
expert
3:00remaining
How does WordPress determine if a user can perform a specific action?

Explain the process WordPress uses internally to decide if a user has permission to perform an action like 'edit_post'. Which of the following best describes this process?

AWordPress checks the user's role and matches it exactly to the action name.
BWordPress checks the user's capabilities, which are assigned to roles or directly to users, to see if the capability required for the action is present.
CWordPress checks if the user is an administrator; if not, all actions are denied.
DWordPress uses the user's username to look up permissions in the database for each action.
Attempts:
2 left
💡 Hint

Think about how roles and capabilities relate to permissions.