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
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
- Scaling
- Constant
- Instructions
- 27–72
- Plugin surface
- 1 hook
- 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 100.
Third-party callbacks on 'rest_pre_dispatch' 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 · 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.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($result) && !is_wp_error() | 27 | apply_filters(), rest_ensure_response(), is_wp_error(), array_pop() |
!empty($result) && is_wp_error() | 31 | apply_filters(), rest_ensure_response(), is_wp_error(), ->error_to_response(), array_pop() |
empty($result) && is_wp_error() | 32 | apply_filters(), ->match_request_to_handler(), is_wp_error(), ->error_to_response(), array_pop() |
empty($result) && is_callable() | 48 | apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), is_wp_error(), ->respond_to_request(), array_pop() |
empty($result) && !is_callable() | 57 | apply_filters(), ->match_request_to_handler(), is_wp_error(), is_callable(), __(), is_wp_error(), ->respond_to_request(), array_pop() |
empty($result) && is_callable() | 57 | apply_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–63 | apply_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() | 66 | apply_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–72 | apply_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 100 | 27–72 | 7 | |
| 8.5 | 100 | 27–72 | 7 | |
| 8.4 | 100 | 27–72 | 7 | |
| 8.3 | 100 | 27–72 | 7 | |
| 8.2 | 100 | 27–72 | 7 | 2 fewer instructions than PHP 8.1 |
| 8.1 | 102 | 27–74 | 7 | |
| 7.4 | 102 | 27–74 | 7 |
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:
- apply_filters( rest_pre_dispatch )filterline 1079 (+16 into the body)
Filters the pre-calculated result of a REST API dispatch request.
Uses · 8
- apply_filters()Calls the callback functions that have been added to a filter hook.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- WP_REST_Server::error_to_response()Converts an error to a response object.
- WP_REST_Server::match_request_to_handler()Matches a request object to its handler.
- WP_Error::__construct()Initializes the error.
- WP_REST_Server::respond_to_request()Dispatches the request to the callback handler.
Used by · 2
- WP_REST_Server::embed_links()Embeds the links from the data into the request.
- WP_REST_Server::serve_request()Handles serving a REST API request.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.