What is a permission callback in WordPress REST API?
A permission callback is a function that checks if the current user has the right to access or modify a REST API endpoint. It returns true if allowed, false otherwise.
Click to reveal answer
beginner
Why do we use permission callbacks when registering REST API routes?
Permission callbacks protect REST API routes by ensuring only authorized users can access or change data, helping keep the site secure.
Click to reveal answer
intermediate
How do you define a permission callback when registering a REST route in WordPress?
You add a 'permission_callback' key in the route arguments, assigning it a function that returns true or false based on user capabilities.
Click to reveal answer
beginner
What happens if a permission callback returns false?
The REST API request is denied with a 403 Forbidden error, preventing unauthorized access.
Click to reveal answer
intermediate
Give an example of a simple permission callback function in WordPress.
function my_permission_callback() { return current_user_can('edit_posts'); } This allows only users who can edit posts to access the route.
Click to reveal answer
What does a permission callback function return to allow access?
Aan error message
Btrue
Cnull
Dfalse
✗ Incorrect
Permission callbacks must return true to allow access to the REST API endpoint.
Where do you specify the permission callback when registering a REST route?
AIn the 'args' argument
BIn the 'callback' argument
CIn the 'methods' argument
DIn the 'permission_callback' argument
✗ Incorrect
The 'permission_callback' argument is used to define the function that checks user permissions.
What HTTP status code is returned if permission callback denies access?
A200 OK
B404 Not Found
C403 Forbidden
D500 Internal Server Error
✗ Incorrect
A 403 Forbidden status code indicates the user is not allowed to access the resource.
Which WordPress function is commonly used inside permission callbacks to check capabilities?
Acurrent_user_can()
Bwp_enqueue_script()
Cget_post()
Dregister_rest_route()
✗ Incorrect
current_user_can() checks if the current user has a specific capability.
If no permission callback is set, what is the default behavior for REST API routes?
AAccess is allowed to everyone
BAccess is denied to everyone
CAccess depends on the HTTP method
DAccess depends on user role
✗ Incorrect
By default, REST API routes are public unless a permission callback restricts access.
Explain what a permission callback is and why it is important in WordPress REST API.
Think about who can use your API and how you check that.
You got /3 concepts.
Describe how to add a permission callback when registering a REST API route in WordPress.
Focus on the arguments passed to register_rest_route.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a permission callback in WordPress REST API routes?
easy
A. To define the URL of the REST API route
B. To check if the current user has permission to access the route
C. To format the response data before sending
D. To register the REST API route with WordPress
Solution
Step 1: Understand permission callback role
A permission callback is a function that decides if a user can access a REST API route.
Step 2: Identify what permission callback checks
It returns true if the user has the right capability, false otherwise.
Final Answer:
To check if the current user has permission to access the route -> Option B
Quick Check:
Permission callback = access control [OK]
Hint: Permission callbacks control access rights [OK]
Common Mistakes:
Confusing permission callback with route registration
Thinking it formats data instead of checking access
Assuming it sets the route URL
2. Which of the following is the correct way to define a permission callback in register_rest_route?
easy
A. "permission_callback" => array(check_user_permission)
B. "permission_callback" => check_user_permission()
C. "permission_callback" => "check_user_permission()"
D. "permission_callback" => check_user_permission
Solution
Step 1: Understand callback syntax in PHP arrays
Callbacks are passed as callable references without parentheses.
Step 2: Identify correct syntax for permission_callback
It should be a string or callable without calling it immediately, so no parentheses.
Final Answer:
"permission_callback" => check_user_permission -> Option D
Quick Check:
Callback = function name without () [OK]
Hint: Use function name without parentheses for callbacks [OK]
Common Mistakes:
Adding parentheses calls the function immediately
Using array() incorrectly for single function
Passing callback as a string with quotes and parentheses
3. Given this permission callback function, what will register_rest_route allow?
Permission callbacks must return true or false explicitly to avoid unexpected behavior.
Final Answer:
Missing return false when user lacks permission -> Option C
Quick Check:
Always return true or false in permission callbacks [OK]
Hint: Always return false if permission denied [OK]
Common Mistakes:
Omitting return false causes implicit null return
Assuming no return means false automatically
Using wrong capability names
5. You want to create a REST API route that only allows users who can either edit_posts or publish_posts. Which permission callback function correctly implements this?
hard
A. function permission_check() {
return current_user_can('edit_posts') || current_user_can('publish_posts');
}
B. function permission_check() {
return current_user_can('publish_posts');
}
C. function permission_check() {
if (current_user_can('edit_posts')) {
return true;
}
return false;
}
D. function permission_check() {
return current_user_can('edit_posts') && current_user_can('publish_posts');
}
Solution
Step 1: Understand the requirement
Access should be granted if user can edit posts OR publish posts.
Step 2: Analyze each function logic
function permission_check() {
return current_user_can('edit_posts') && current_user_can('publish_posts');
} requires both capabilities (AND), which is too strict. function permission_check() {
return current_user_can('edit_posts') || current_user_can('publish_posts');
} uses OR, which matches requirement. function permission_check() {
if (current_user_can('edit_posts')) {
return true;
}
return false;
} only checks edit_posts. function permission_check() {
return current_user_can('publish_posts');
} only checks publish_posts.
Final Answer:
function permission_check() {
return current_user_can('edit_posts') || current_user_can('publish_posts');
} -> Option A
Quick Check:
Use OR for either capability allowed [OK]
Hint: Use || to allow either capability [OK]
Common Mistakes:
Using && instead of || for either/or logic
Checking only one capability when two are needed
Not returning false explicitly when needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.