Challenge - 5 Problems
WordPress REST API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this WordPress REST API callback?
Consider this REST API endpoint callback in WordPress. What will the response contain when accessed?
Wordpress
<?php add_action('rest_api_init', function () { register_rest_route('myplugin/v1', '/data', array( 'methods' => 'GET', 'callback' => function () { return new WP_REST_Response(array('message' => 'Hello World'), 200); }, )); });
Attempts:
2 left
💡 Hint
WP_REST_Response formats the array as JSON with the given status code.
✗ Incorrect
The callback returns a WP_REST_Response object with data and status 200. WordPress converts this to JSON with the message key.
📝 Syntax
intermediate2:00remaining
Which option correctly registers a POST REST API route in WordPress?
You want to create a REST API route that accepts POST requests at 'myplugin/v1/submit'. Which code snippet is correct?
Attempts:
2 left
💡 Hint
The key for HTTP verbs is 'methods' and it is case sensitive.
✗ Incorrect
The correct key is 'methods' (plural) and the value must be uppercase 'POST'. Option B uses the correct syntax.
🔧 Debug
advanced2:00remaining
Why does this REST API callback cause a 500 Internal Server Error?
Examine this callback code for a WordPress REST API route. Why does it cause a server error?
Wordpress
<?php
function my_callback(WP_REST_Request $request) {
$param = $request->get_param('id');
if (!$param) {
return new WP_Error('no_id', 'ID parameter missing', array('status' => 400));
}
return array('id' => $param);
}
add_action('rest_api_init', function () {
register_rest_route('myplugin/v1', '/item', array(
'methods' => 'GET',
'callback' => 'my_callback',
));
});Attempts:
2 left
💡 Hint
WordPress REST API requires permission_callback for routes unless explicitly allowed.
✗ Incorrect
Without a permission_callback, WordPress denies access causing a 500 error. Returning WP_Error is correct but permission_callback is mandatory.
❓ state_output
advanced2:00remaining
What is the HTTP status code returned by this REST API callback?
This callback returns a WP_Error with a custom status. What status code will the client receive?
Wordpress
<?php
function error_callback() {
return new WP_Error('forbidden', 'You do not have access', array('status' => 403));
}
add_action('rest_api_init', function () {
register_rest_route('myplugin/v1', '/secret', array(
'methods' => 'GET',
'callback' => 'error_callback',
'permission_callback' => '__return_true',
));
});Attempts:
2 left
💡 Hint
WP_Error with 'status' in data sets the HTTP status code.
✗ Incorrect
WP_Error with 'status' => 403 causes WordPress to send HTTP 403 Forbidden status code.
🧠 Conceptual
expert2:00remaining
Which statement about WordPress REST API request and response handling is TRUE?
Select the correct statement about how WordPress handles REST API requests and responses.
Attempts:
2 left
💡 Hint
Think about default behaviors for arrays and errors in WordPress REST API.
✗ Incorrect
Returning a plain array is converted to JSON with HTTP 200 by default. Permission_callback is required but missing it causes 500 error, not 403. WP_REST_Response can set headers. WP_Error status depends on 'status' field, not always 500.