Title: WP_REST_Server::dispatch
Published: December 9, 2015
Last modified: August 20, 2026

---

# WP_REST_Server::dispatch( WP_REST_Request $request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#wp--skip-link--target)

Matches the request to a callback and call it.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#parameters)󠁿

 `$request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

Request to attempt dispatching.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
Response returned by the callback.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#source)󠁿

    ```php
    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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/class-wp-rest-server.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rest-api/class-wp-rest-server.php#L1063)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/class-wp-rest-server.php#L1063-L1129)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#hooks)󠁿

 [apply_filters( ‘rest_pre_dispatch’, mixed $result, WP_REST_Server $server, WP_REST_Request $request )](https://developer.wordpress.org/reference/hooks/rest_pre_dispatch/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_REST_Server::match_request_to_handler()](https://developer.wordpress.org/reference/classes/wp_rest_server/match_request_to_handler/)`wp-includes/rest-api/class-wp-rest-server.php` |

Matches a request object to its handler.

  | 
| [WP_REST_Server::respond_to_request()](https://developer.wordpress.org/reference/classes/wp_rest_server/respond_to_request/)`wp-includes/rest-api/class-wp-rest-server.php` |

Dispatches the request to the callback handler.

  | 
| [WP_REST_Server::error_to_response()](https://developer.wordpress.org/reference/classes/wp_rest_server/error_to_response/)`wp-includes/rest-api/class-wp-rest-server.php` |

Converts an error to a response object.

  | 
| [rest_ensure_response()](https://developer.wordpress.org/reference/functions/rest_ensure_response/)`wp-includes/rest-api.php` |

Ensures a REST response is a response object (for consistency).

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 5 more](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#)

| Used by | Description | 
| [WP_REST_Server::serve_request()](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_request/)`wp-includes/rest-api/class-wp-rest-server.php` |

Handles serving a REST API request.

  | 
| [WP_REST_Server::embed_links()](https://developer.wordpress.org/reference/classes/wp_rest_server/embed_links/)`wp-includes/rest-api/class-wp-rest-server.php` |

Embeds the links from the data into the request.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/WP_REST_Server/dispatch/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_server%2Fdispatch%2F)
before being able to contribute a note or feedback.