wppaste
WordPress

WP_REST_Server::dispatch( WP_REST_Request $request ): WP_REST_Response

Since
4.4.0
Source
wp-includes/rest-api/class-wp-rest-server.php:1063
Matches the request to a callback and call it.

Compatibility

WordPress
since 4.4.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
Request to attempt dispatching.

Return value

WP_REST_Response
Response returned by the callback.

Performance profile

How much work a call to WP_REST_Server::dispatch() 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
27–72

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

Plugin surface
1 hook

Third-party callbacks on 'rest_pre_dispatch' 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 · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Server::dispatch() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!empty($result) && !is_wp_error()27apply_filters(), rest_ensure_response(), is_wp_error(), array_pop()
!empty($result) && is_wp_error()31apply_filters(), rest_ensure_response(), is_wp_error(), ->error_to_response(), array_pop()
empty($result) && is_wp_error()32apply_filters(), ->match_request_to_handler(), is_wp_error(), ->error_to_response(), array_pop()
empty($result) && is_callable()48apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), is_wp_error(), ->respond_to_request(), array_pop()
empty($result) && !is_callable()57apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), __(), is_wp_error(), ->respond_to_request(), array_pop()
empty($result) && is_callable()57apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), is_wp_error(), ->has_valid_params(), is_wp_error(), ->respond_to_request(), array_pop()
empty($result) && !is_wp_error() && is_callable()62–63apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), is_wp_error(), ->has_valid_params(), is_wp_error(), ->sanitize_params(), is_wp_error(), ->respond_to_request(), array_pop()
empty($result) && !is_callable()66apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), __(), is_wp_error(), ->has_valid_params(), is_wp_error(), ->respond_to_request(), array_pop()
empty($result) && !is_wp_error() && !is_callable()71–72apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), __(), is_wp_error(), ->has_valid_params(), is_wp_error(), ->sanitize_params(), is_wp_error(), ->respond_to_request(), array_pop()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10027–727
8.510027–727
8.410027–727
8.310027–727
8.210027–7272 fewer instructions than PHP 8.1
8.110227–747
7.410227–747

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 · 1

One hook fires while WP_REST_Server::dispatch() runs, in this order:

  1. apply_filters( rest_pre_dispatch )filterline 1079 (+16 into the body)

    Filters the pre-calculated result of a REST API dispatch request.

Uses · 8

Used by · 2

Source code

	public function dispatch( $request ) {		$this->dispatching_requests[] = $request; 		/**		 * Filters the pre-calculated result of a REST API dispatch request.		 *		 * Allow hijacking the request before dispatching by returning a non-empty. The returned value		 * will be used to serve the request instead.		 *		 * @since 4.4.0		 *		 * @param mixed           $result  Response to replace the requested version with. Can be anything		 *                                 a normal endpoint can return, or null to not hijack the request.		 * @param WP_REST_Server  $server  Server instance.		 * @param WP_REST_Request $request Request used to generate the response.		 */		$result = apply_filters( 'rest_pre_dispatch', null, $this, $request ); 		if ( ! empty( $result ) ) { 			// Normalize to either WP_Error or WP_REST_Response...			$result = rest_ensure_response( $result ); 			// ...then convert WP_Error across.			if ( is_wp_error( $result ) ) {				$result = $this->error_to_response( $result );			} 			array_pop( $this->dispatching_requests );			return $result;		} 		$error   = null;		$matched = $this->match_request_to_handler( $request ); 		if ( is_wp_error( $matched ) ) {			$response = $this->error_to_response( $matched );			array_pop( $this->dispatching_requests );			return $response;		} 		list( $route, $handler ) = $matched; 		if ( ! is_callable( $handler['callback'] ) ) {			$error = new WP_Error(				'rest_invalid_handler',				__( 'The handler for the route is invalid.' ),				array( 'status' => 500 )			);		} 		if ( ! is_wp_error( $error ) ) {			$check_required = $request->has_valid_params();			if ( is_wp_error( $check_required ) ) {				$error = $check_required;			} else {				$check_sanitized = $request->sanitize_params();				if ( is_wp_error( $check_sanitized ) ) {					$error = $check_sanitized;				}			}		} 		$response = $this->respond_to_request( $request, $route, $handler, $error );		array_pop( $this->dispatching_requests );		return $response;	}

Changelog

Introduced in 4.4.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.