rest_handle_options_request

Handles OPTIONS requests for the server.

Description

(WP_REST_Response) rest_handle_options_request( (mixed) $response, (WP_REST_Server) $handler, (WP_REST_Request) $request ); 

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

Returns (WP_REST_Response)

Modified response, either response or `null` to indicate pass-through.

Parameters (3)

0. $response (mixed)
Current response, either response or null to indicate pass-through.
1. $handler (WP_REST_Server)
ResponseHandler instance (usually WP_REST_Server).
2. $request (WP_REST_Request)
The request that was used to make current response.

Usage

  1. if ( !function_exists( 'rest_handle_options_request' ) ) { 
  2. require_once ABSPATH . WPINC . '/rest-api.php'; 
  3.  
  4. // Current response, either response or `null` to indicate pass-through. 
  5. $response = null; 
  6.  
  7. // ResponseHandler instance (usually WP_REST_Server). 
  8. $handler = null; 
  9.  
  10. // The request that was used to make current response. 
  11. $request = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = rest_handle_options_request($response, $handler, $request); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/rest-api.php  
  1. function rest_handle_options_request( $response, $handler, $request ) { 
  2. if ( ! empty( $response ) || $request->get_method() !== 'OPTIONS' ) { 
  3. return $response; 
  4.  
  5. $response = new WP_REST_Response(); 
  6. $data = array(); 
  7.  
  8. foreach ( $handler->get_routes() as $route => $endpoints ) { 
  9. $match = preg_match( '@^' . $route . '$@i', $request->get_route() ); 
  10.  
  11. if ( ! $match ) { 
  12. continue; 
  13.  
  14. $data = $handler->get_data_for_route( $route, $endpoints, 'help' ); 
  15. $response->set_matched_route( $route ); 
  16. break; 
  17.  
  18. $response->set_data( $data ); 
  19. return $response;