Challenge - 5 Problems
WordPress REST API Endpoint 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 endpoint registration?
Consider the following code that registers a custom REST API endpoint in WordPress. What will be the JSON response when a GET request is made to
/wp-json/custom/v1/message?Wordpress
<?php add_action('rest_api_init', function () { register_rest_route('custom/v1', '/message', [ 'methods' => 'GET', 'callback' => function () { return ['greeting' => 'Hello, world!']; }, ]); });
Attempts:
2 left
💡 Hint
Look at the callback return value and the route path.
✗ Incorrect
The callback returns an array with key 'greeting' and value 'Hello, world!'. WordPress converts this to JSON automatically for the REST API response.
📝 Syntax
intermediate2:00remaining
Which option correctly registers a POST method for a custom REST API endpoint?
You want to register a REST API endpoint in WordPress that accepts POST requests at
/wp-json/custom/v1/data. Which code snippet correctly registers this endpoint?Attempts:
2 left
💡 Hint
Check the key name and case sensitivity for HTTP methods.
✗ Incorrect
The key must be 'methods' (plural) and the method name must be uppercase 'POST'. Option D uses correct syntax.
🔧 Debug
advanced2:00remaining
Why does this custom endpoint return a 404 error?
This code registers a custom REST API endpoint, but when accessing
/wp-json/custom/v1/info, it returns a 404 error. What is the cause?Wordpress
<?php add_action('rest_api_init', function () { register_rest_route('custom/v1', 'info', [ 'methods' => 'GET', 'callback' => function () { return ['info' => 'Details here']; }, ]); });
Attempts:
2 left
💡 Hint
Check the route path format in register_rest_route.
✗ Incorrect
The route path must start with a slash '/' to be recognized correctly. Without it, WordPress does not match the endpoint and returns 404.
❓ state_output
advanced2:00remaining
What is the output of this endpoint with permission callback?
Given this code, what will be the HTTP status code when an unauthenticated user requests
/wp-json/custom/v1/secret?Wordpress
<?php add_action('rest_api_init', function () { register_rest_route('custom/v1', '/secret', [ 'methods' => 'GET', 'callback' => function () { return ['secret' => '42']; }, 'permission_callback' => function () { return is_user_logged_in(); }, ]); });
Attempts:
2 left
💡 Hint
Consider what happens when permission_callback returns false.
✗ Incorrect
If permission_callback returns false, WordPress returns 403 Forbidden, not 401 or 404.
🧠 Conceptual
expert3:00remaining
Which statement about custom REST API endpoint registration in WordPress is TRUE?
Select the one correct statement about registering custom REST API endpoints in WordPress.
Attempts:
2 left
💡 Hint
Think about when and how endpoints are registered and route path format.
✗ Incorrect
Endpoints must be registered on 'rest_api_init' hook, and route paths require a leading slash. Permission callbacks are optional but if omitted, endpoint is accessible. Callback returns data, not WP_Error for success.