rest_handle_options_request() WordPress Function

The rest_handle_options_request() function is used to handle OPTIONS requests for the WordPress REST API. This function is called by the rest_api_loaded() function.

rest_handle_options_request( mixed $response, WP_REST_Server $handler, WP_REST_Request $request ) #

Handles OPTIONS requests for the server.


Description

This is handled outside of the server code, as it doesn’t obey normal route mapping.


Top ↑

Parameters

$response

(mixed)(Required)Current response, either response or null to indicate pass-through.

$handler

(WP_REST_Server)(Required)ResponseHandler instance (usually WP_REST_Server).

$request

(WP_REST_Request)(Required)The request that was used to make current response.


Top ↑

Return

(WP_REST_Response) Modified response, either response or null to indicate pass-through.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_handle_options_request( $response, $handler, $request ) {
	if ( ! empty( $response ) || $request->get_method() !== 'OPTIONS' ) {
		return $response;
	}

	$response = new WP_REST_Response();
	$data     = array();

	foreach ( $handler->get_routes() as $route => $endpoints ) {
		$match = preg_match( '@^' . $route . '$@i', $request->get_route(), $matches );

		if ( ! $match ) {
			continue;
		}

		$args = array();
		foreach ( $matches as $param => $value ) {
			if ( ! is_int( $param ) ) {
				$args[ $param ] = $value;
			}
		}

		foreach ( $endpoints as $endpoint ) {
			// Remove the redundant preg_match() argument.
			unset( $args[0] );

			$request->set_url_params( $args );
			$request->set_attributes( $endpoint );
		}

		$data = $handler->get_data_for_route( $route, $endpoints, 'help' );
		$response->set_matched_route( $route );
		break;
	}

	$response->set_data( $data );
	return $response;
}

Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More