rest_send_allow_header() WordPress Function

The rest_send_allow_header() function is a utility function that sends an Allow header for the specified REST API requests. This function is useful for specifying which HTTP methods are allowed for a given resource.

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

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


Parameters

$response

(WP_REST_Response)(Required)Current response being served.

$server

(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) Response to be served, with "Allow" header if route has allowed methods.


Top ↑

Source

File: wp-includes/rest-api.php

788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
function rest_send_allow_header( $response, $server, $request ) {
    $matched_route = $response->get_matched_route();
 
    if ( ! $matched_route ) {
        return $response;
    }
 
    $routes = $server->get_routes();
 
    $allowed_methods = array();
 
    // Get the allowed methods across the routes.
    foreach ( $routes[ $matched_route ] as $_handler ) {
        foreach ( $_handler['methods'] as $handler_method => $value ) {
 
            if ( ! empty( $_handler['permission_callback'] ) ) {
 
                $permission = call_user_func( $_handler['permission_callback'], $request );
 
                $allowed_methods[ $handler_method ] = true === $permission;
            } else {
                $allowed_methods[ $handler_method ] = true;
            }
        }
    }
 
    // Strip out all the methods that are not allowed (false values).
    $allowed_methods = array_filter( $allowed_methods );
 
    if ( $allowed_methods ) {
        $response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) );
    }
 
    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