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?
if ( current_user_can('edit_posts') ) { echo 'You can edit posts!'; } else { echo 'Access denied.'; }
Think about what current_user_can() returns when the user lacks the capability.
The function current_user_can('edit_posts') returns false if the user does not have that capability. So the else block runs, printing 'Access denied.'
Choose the correct PHP code snippet that checks if the current user has the 'manage_options' capability.
Remember the correct function name and syntax for capability checks.
The correct function is current_user_can() with the capability as a string argument. Option A uses this correctly.
Option A uses a wrong function name. Option A assigns instead of compares. Option A checks for false, which is valid but not the direct check asked.
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?
if ( is_user_logged_in() && current_user_can('publish_posts') ) { echo 'Welcome, author!'; } else { echo 'Please log in.'; }
Consider how is_user_logged_in() affects the condition.
If the user is not logged in, is_user_logged_in() returns false, so the whole condition is false and the else block runs, printing 'Please log in.'
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?
if ( current_user_can( 'edit_post' ) ) { echo 'Can edit post'; } else { echo 'Cannot edit post'; }
Check the exact capability string spelling.
The capability 'edit_post' does not exist in WordPress. The correct capability is 'edit_posts' (plural). So the check always fails.
Choose the correct statement about how WordPress handles user capability checks.
Think about the function's behavior when no user is logged in.
current_user_can() checks capabilities only for the current logged-in user. If no user is logged in, it returns false. It does not accept a user ID argument. Capabilities are stored serialized in the database and checked internally. It never throws fatal errors for missing capabilities.