WP_REST_Request::get_method()

Retrieves the HTTP method for the request.


Return

(string) HTTP method.


Top ↑

Source

File: wp-includes/rest-api/class-wp-rest-request.php

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

Top ↑

Changelog

Changelog
Version Description
4.4.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Casey Milne

    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.