WP_REST_Request::get_method(): string

Retrieves the HTTP method for the request.

Return

string HTTP method.

Source

public function get_method() {
	return $this->method;
}

Changelog

VersionDescription
4.4.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The return value (HTTP Method) is returned in full uppercase such as “POST”, “GET”, “PATCH”, “DELETE”. As an example in your API route callback you can check for the method type when you’re expecting one of several potential methods:

    if ( 'POST' === $request->get_method() ) {
    
      // Do POST specific processing.
    
    }

    Alternatively you might use a switch to check the method such as:

    switch ( $request->method() ) {
    
      case 'POST':
        $this->do_post_processing( $request );
        break;
    
      case 'GET':
        $this->do_get_processing( $request );
        break;
    
    }

    These examples are in the context of a callback from register_rest_route() which passes the $request (instance of WP_REST_Request) to the callback.

You must log in before being able to contribute a note or feedback.