0
0
Wordpressframework~20 mins

User capability checks in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
User Capability 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 without 'edit_posts' capability tries to access this code?

Consider this WordPress PHP snippet inside a plugin or theme:

if ( current_user_can('edit_posts') ) {
    echo 'You can edit posts!';
} else {
    echo 'Access denied.';
}

What will be the output if the current user does NOT have the 'edit_posts' capability?

Wordpress
if ( current_user_can('edit_posts') ) {
    echo 'You can edit posts!';
} else {
    echo 'Access denied.';
}
AYou can edit posts!
BFatal error: undefined function current_user_can()
CAccess denied.
DNo output at all
Attempts:
2 left
💡 Hint

Think about what current_user_can() returns when the user lacks the capability.

📝 Syntax
intermediate
2:00remaining
Which option correctly checks if a user can 'manage_options' in WordPress?

Choose the correct PHP code snippet that checks if the current user has the 'manage_options' capability.

Aif (current_user_can('manage_options')) { /* code */ }
Bif (user_can('manage_options')) { /* code */ }
Cif (current_user_can = 'manage_options') { /* code */ }
Dif (current_user_can('manage_options') == false) { /* code */ }
Attempts:
2 left
💡 Hint

Remember the correct function name and syntax for capability checks.

state_output
advanced
2:00remaining
What is the output of this code when the user is logged out?

Analyze this WordPress PHP snippet:

if ( is_user_logged_in() && current_user_can('publish_posts') ) {
    echo 'Welcome, author!';
} else {
    echo 'Please log in.';
}

What will it output if the user is NOT logged in?

Wordpress
if ( is_user_logged_in() && current_user_can('publish_posts') ) {
    echo 'Welcome, author!';
} else {
    echo 'Please log in.';
}
AWelcome, author!
BPlease log in.
CNo output
DFatal error: call to current_user_can() without user
Attempts:
2 left
💡 Hint

Consider how is_user_logged_in() affects the condition.

🔧 Debug
advanced
2:00remaining
Why does this capability check always fail?

Look at this code snippet:

if ( current_user_can( 'edit_post' ) ) {
    echo 'Can edit post';
} else {
    echo 'Cannot edit post';
}

Why does it always print 'Cannot edit post' even for users who can edit posts?

Wordpress
if ( current_user_can( 'edit_post' ) ) {
    echo 'Can edit post';
} else {
    echo 'Cannot edit post';
}
AThe echo statements are reversed.
BThe function current_user_can() requires a user ID as second argument.
CThe code is missing a call to wp_get_current_user() before checking.
DThe capability name 'edit_post' is incorrect; it should be 'edit_posts'.
Attempts:
2 left
💡 Hint

Check the exact capability string spelling.

🧠 Conceptual
expert
2:00remaining
Which statement about user capability checks in WordPress is TRUE?

Choose the correct statement about how WordPress handles user capability checks.

Acurrent_user_can() checks capabilities only for the currently logged-in user and returns false if no user is logged in.
Bcurrent_user_can() can check capabilities for any user by passing a user ID as the second argument.
CUser capabilities are stored in the database as plain text and checked directly by current_user_can().
DIf a user lacks a capability, current_user_can() throws a fatal error.
Attempts:
2 left
💡 Hint

Think about the function's behavior when no user is logged in.