wppaste
WordPress

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:1238
Dispatches the request to the callback handler.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
41–84

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 92.

Plugin surface
3 hooks

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.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always41–43apply_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–44apply_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 !== null53–55apply_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 !== null54–56apply_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–56apply_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–57apply_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 === null56–58apply_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 === null57–59apply_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 !== null66–68apply_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–68apply_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 !== null67–69apply_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–69apply_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 === null69–71apply_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 === null70–72apply_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 !== null78–80apply_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 !== null79–81apply_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 === null81–83apply_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 === null82–84apply_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:

  1. apply_filters( rest_request_before_callbacks )filterline 1256 (+18 into the body)

    Filters the response before executing any REST API callbacks.

  2. apply_filters( rest_dispatch_request )filterline 1287 (+49 into the body)

    Filters the REST API dispatch request result.

  3. apply_filters( rest_request_after_callbacks )filterline 1318 (+80 into the body)

    Filters the response immediately after executing any REST API callbacks.

Uses · 7

Used by · 2

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.