rest_send_allow_header

Sends the "Allow" header to state all methods that can be sent to the current route.

Description

(WP_REST_Response) rest_send_allow_header( (WP_REST_Response) $response, (WP_REST_Server) $server, (WP_REST_Request) $request ); 

Returns (WP_REST_Response)

Response to be served, with "Allow" header if route has allowed methods.

Parameters (3)

0. $response (WP_REST_Response)
Current response being served.
1. $server (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_send_allow_header' ) ) { 
  2. require_once ABSPATH . WPINC . '/rest-api.php'; 
  3.  
  4. // Current response being served. 
  5. $response = null; 
  6.  
  7. // ResponseHandler instance (usually WP_REST_Server). 
  8. $server = 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_send_allow_header($response, $server, $request); 
  15.  

Defined (1)

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

/wp-includes/rest-api.php  
  1. function rest_send_allow_header( $response, $server, $request ) { 
  2. $matched_route = $response->get_matched_route(); 
  3.  
  4. if ( ! $matched_route ) { 
  5. return $response; 
  6.  
  7. $routes = $server->get_routes(); 
  8.  
  9. $allowed_methods = array(); 
  10.  
  11. // Get the allowed methods across the routes. 
  12. foreach ( $routes[ $matched_route ] as $_handler ) { 
  13. foreach ( $_handler['methods'] as $handler_method => $value ) { 
  14.  
  15. if ( ! empty( $_handler['permission_callback'] ) ) { 
  16.  
  17. $permission = call_user_func( $_handler['permission_callback'], $request ); 
  18.  
  19. $allowed_methods[ $handler_method ] = true === $permission; 
  20. } else { 
  21. $allowed_methods[ $handler_method ] = true; 
  22.  
  23. // Strip out all the methods that are not allowed (false values). 
  24. $allowed_methods = array_filter( $allowed_methods ); 
  25.  
  26. if ( $allowed_methods ) { 
  27. $response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) ); 
  28.  
  29. return $response;