rest_post_dispatch
Allows you to modify the REST API response after the route handler has finished executing but before the response is sent to the client.
The filter can modify the response data, HTTP status, and headers.
The filter also applies to internal requests for preloading data, _embed embedded resources, and individual requests inside /batch/v1.
The filter may be called multiple times during a single HTTP request. Check the route and request method before modifying the response.
To add a persistent field to REST API objects, use register_rest_field(). The function adds the field to the object schema and works correctly with the _fields parameter.
Usage
add_filter( 'rest_post_dispatch', 'wp_kama_rest_post_dispatch_filter', 10, 3 );
/**
* Function for `rest_post_dispatch` filter-hook.
*
* @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`.
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request Request used to generate the response.
*
* @return WP_HTTP_Response
*/
function wp_kama_rest_post_dispatch_filter( $result, $server, $request ){
// filter...
return $result;
}
- $result(WP_HTTP_Response)
- The response that will be sent to the client. Usually an WP_REST_Response{} object.
- $server(WP_REST_Server)
- The current REST server instance.
- $request(WP_REST_Request)
- The request for which the response was created. Allows you to get the route, HTTP method, and request parameters.
Examples
#1 Adding data to a custom route response
Let's add the response generation time for the /my-plugin/v1/status route.
add_filter( 'rest_post_dispatch', 'wp_kama_add_generated_time', 10, 3 );
function wp_kama_add_generated_time( $result, $server, $request ) {
if (
'/my-plugin/v1/status' !== $request->get_route()
|| 'GET' !== $request->get_method()
) {
return $result;
}
$data = $result->get_data();
if ( is_array( $data ) ) {
$data['generated_at'] = gmdate( 'c' );
$result->set_data( $data );
}
return $result;
}Changelog
| Since 4.4.0 | Introduced. |
| Since 4.5.0 | Applied to embedded responses. |
Where the hook is called
rest_post_dispatch
rest_post_dispatch
rest_post_dispatch
rest_post_dispatch
wp-includes/rest-api/class-wp-rest-server.php 464
$result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request );
wp-includes/rest-api.php 3033
$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $server, $request );
wp-includes/rest-api/class-wp-rest-server.php 824
$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request );
wp-includes/rest-api/class-wp-rest-server.php 1894
$result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $single_request );
Where the hook is used in WordPress
wp-includes/connectors.php 753
add_filter( 'rest_post_dispatch', '_wp_connectors_rest_settings_dispatch', 10, 3 );
wp-includes/rest-api.php 253
add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
wp-includes/rest-api.php 254
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );