Responses
When building an API sometimes we might want to return a response directly to the client. For example:
<?php
$router->get('/posts/(?P<ID>[\d]+)', function (int $ID) {
return get_post($ID);
})
->returns(Post::class); /* (1) */
- A 422 HTTP error will be raised if unable to find a post because the data structure retrieved by the
*get_post*function will not match the expected response structure e.g. Post::class.
Early return¶
To avoid those scenarios we can either return a WP_Error or a WP_REST_Response.
Difference between returning WP_REST_Response or WP_Error¶
The main difference between returning a WP_Error or a WP_REST_Response is regarding the JSON returned in the body.