WP_REST_Server::respond_to_request( WP_REST_Request $request, string $route, array $handler, WP_Error|null $response ): WP_REST_Response
- Since
- 5.6.0
- Source
wp-includes/rest-api/class-wp-rest-server.php:1237
Compatibility
- WordPress
- since 5.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$requestWP_REST_Request- The request object.
$routestring- The matched route regex.
$handlerarray- The matched route handler.
$responseWP_Error|null- The current error object if any.
Return value
WP_REST_Response
Performance profile
How much work a call to WP_REST_Server::respond_to_request() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 41–84
- Plugin surface
- 3 hooks
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 92.
Third-party callbacks on 'rest_request_before_callbacks', 'rest_dispatch_request', 'rest_request_after_callbacks' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Server::respond_to_request() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 41–43 | apply_filters(), is_wp_error(), is_wp_error(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
is_wp_error() | 42–44 | apply_filters(), is_wp_error(), is_wp_error(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
!is_wp_error() && $dispatch_result !== null | 53–55 | apply_filters(), is_wp_error(), is_wp_error(), apply_filters(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
$dispatch_result !== null | 54–56 | apply_filters(), is_wp_error(), is_wp_error(), apply_filters(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) | 54–56 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), is_wp_error(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) | 55–57 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), is_wp_error(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
!is_wp_error() && $dispatch_result === null | 56–58 | apply_filters(), is_wp_error(), is_wp_error(), apply_filters(), call_user_func(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
$dispatch_result === null | 57–59 | apply_filters(), is_wp_error(), is_wp_error(), apply_filters(), call_user_func(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
!is_wp_error() && !empty($handler) && $dispatch_result !== null | 66–68 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), is_wp_error(), apply_filters(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) | 66–68 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), __(), rest_authorization_required_code(), status(), is_wp_error(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) && $dispatch_result !== null | 67–69 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), is_wp_error(), apply_filters(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) | 67–69 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), __(), rest_authorization_required_code(), status(), is_wp_error(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
6 further outcomes, up to 84 instructions
!is_wp_error() && !empty($handler) && $dispatch_result === null | 69–71 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), is_wp_error(), apply_filters(), call_user_func(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) && $dispatch_result === null | 70–72 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), is_wp_error(), apply_filters(), call_user_func(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
!is_wp_error() && !empty($handler) && $dispatch_result !== null | 78–80 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), __(), rest_authorization_required_code(), status(), is_wp_error(), apply_filters(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) && $dispatch_result !== null | 79–81 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), __(), rest_authorization_required_code(), status(), is_wp_error(), apply_filters(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
!is_wp_error() && !empty($handler) && $dispatch_result === null | 81–83 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), __(), rest_authorization_required_code(), status(), is_wp_error(), apply_filters(), call_user_func(), apply_filters(), is_wp_error(), rest_ensure_response(), ->set_matched_route(), ->set_matched_handler() |
!empty($handler) && $dispatch_result === null | 82–84 | apply_filters(), is_wp_error(), call_user_func(), is_wp_error(), __(), rest_authorization_required_code(), status(), is_wp_error(), apply_filters(), call_user_func(), apply_filters(), is_wp_error(), ->error_to_response(), ->set_matched_route(), ->set_matched_handler() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 92 instructions, 41–84 executed per call, 8 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 3
3 hooks fire while WP_REST_Server::respond_to_request() runs, in this order:
- apply_filters( rest_request_before_callbacks )filterline 1255 (+18 into the body)
Filters the response before executing any REST API callbacks.
- apply_filters( rest_dispatch_request )filterline 1286 (+49 into the body)
Filters the REST API dispatch request result.
- apply_filters( rest_request_after_callbacks )filterline 1317 (+80 into the body)
Filters the response immediately after executing any REST API callbacks.
Uses · 7
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- WP_Error::__construct()Initializes the error.
- WP_REST_Server::error_to_response()Converts an error to a response object.
Used by · 2
- WP_REST_Server::dispatch()Matches the request to a callback and call it.
- WP_REST_Server::serve_batch_request_v1()Serves the batch/v1 request.
Source code
protected function respond_to_request( $request, $route, $handler, $response ) { /** * Filters the response before executing any REST API callbacks. * * Allows plugins to perform additional validation after a * request is initialized and matched to a registered route, * but before it is executed. * * Note that this filter will not be called for requests that * fail to authenticate or match to a registered route. * * @since 4.7.0 * * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. * Usually a WP_REST_Response or WP_Error. * @param array $handler Route handler used for the request. * @param WP_REST_Request $request Request used to generate the response. */ $response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request ); // Check permission specified on the route. if ( ! is_wp_error( $response ) && ! empty( $handler['permission_callback'] ) ) { $permission = call_user_func( $handler['permission_callback'], $request ); if ( is_wp_error( $permission ) ) { $response = $permission; } elseif ( false === $permission || null === $permission ) { $response = new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to do that.' ), array( 'status' => rest_authorization_required_code() ) ); } } if ( ! is_wp_error( $response ) ) { /** * Filters the REST API dispatch request result. * * Allow plugins to override dispatching the request. * * @since 4.4.0 * @since 4.5.0 Added `$route` and `$handler` parameters. * * @param mixed $dispatch_result Dispatch result, will be used if not empty. * @param WP_REST_Request $request Request used to generate the response. * @param string $route Route matched for the request. * @param array $handler Route handler used for the request. */ $dispatch_result = apply_filters( 'rest_dispatch_request', null, $request, $route, $handler ); // Allow plugins to halt the request via this filter. if ( null !== $dispatch_result ) { $response = $dispatch_result; } else { $response = call_user_func( $handler['callback'], $request ); } } /** * Filters the response immediately after executing any REST API * callbacks. * * Allows plugins to perform any needed cleanup, for example, * to undo changes made during the {@see 'rest_request_before_callbacks'} * filter. * * Note that this filter will not be called for requests that * fail to authenticate or match to a registered route. * * Note that an endpoint's `permission_callback` can still be * called after this filter - see `rest_send_allow_header()`. * * @since 4.7.0 * * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. * Usually a WP_REST_Response or WP_Error. * @param array $handler Route handler used for the request. * @param WP_REST_Request $request Request used to generate the response. */Changelog
Introduced in 5.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/rest-api/class-wp-rest-server.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.