0
0
Wordpressframework~20 mins

Request and response handling in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WordPress REST API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
    },
  ));
});
AA PHP error because WP_REST_Response is undefined
BAn empty JSON object {} with HTTP status 404
CA plain text response 'Hello World' with HTTP status 200
D{"message":"Hello World"} with HTTP status 200
Attempts:
2 left
💡 Hint
WP_REST_Response formats the array as JSON with the given status code.
📝 Syntax
intermediate
2: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?
Aregister_rest_route('myplugin/v1', '/submit', array('methods' => 'post', 'callback' => 'handle_submit'));
Bregister_rest_route('myplugin/v1', '/submit', array('methods' => 'POST', 'callback' => 'handle_submit'));
Cregister_rest_route('myplugin/v1', '/submit', array('methods' => ['GET', 'POST'], 'callback' => 'handle_submit'));
Dregister_rest_route('myplugin/v1', '/submit', array('method' => 'POST', 'callback' => 'handle_submit'));
Attempts:
2 left
💡 Hint
The key for HTTP verbs is 'methods' and it is case sensitive.
🔧 Debug
advanced
2: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',
  ));
});
AThe callback returns an array instead of WP_REST_Response or WP_Error, causing the error.
BThe callback uses get_param() which is not a valid method of WP_REST_Request.
CThe callback returns WP_Error correctly, but the route is missing permission_callback causing 500 error.
DThe callback returns WP_Error and array correctly; the 500 error is caused by missing permission_callback.
Attempts:
2 left
💡 Hint
WordPress REST API requires permission_callback for routes unless explicitly allowed.
state_output
advanced
2: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',
  ));
});
A403 Forbidden
B200 OK
C500 Internal Server Error
D401 Unauthorized
Attempts:
2 left
💡 Hint
WP_Error with 'status' in data sets the HTTP status code.
🧠 Conceptual
expert
2: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.
AReturning a plain array from a callback automatically converts it to JSON with status 200.
BAll REST API routes must have a permission_callback; otherwise, WordPress returns 403 Forbidden automatically.
CWP_REST_Response objects cannot set custom HTTP headers in the response.
DWP_Error objects returned from callbacks always cause a 500 Internal Server Error.
Attempts:
2 left
💡 Hint
Think about default behaviors for arrays and errors in WordPress REST API.