wordpress create REST Api

Example 1

Example 2 – Passing parameter

Example 3 – Passing multiple parameters

Example 3- permission check

 

$request in call back method is an object of ‘WP_REST_Request’ class this class contains methods like

get_header($key)

get_headers()

get_method()

etc.

we can call these methods with the help of $request  object eg:- $request->get_headers()

 

 

Ref: https://developer.wordpress.org/reference/functions/register_rest_route/

1 Like
Author: bm on June 9, 2016
Category: wordpress

2 thoughts on “wordpress create REST Api

  1. Hi,

    I implemented Example 1 webservie to my wordpress version 4.0. It returns [{“code”:”json_no_route”,”message”:”No route was found matching the URL and request method”}]

    Please advise to fix this issue, or how to pass parameters in url.

    Thanks in advance

    1. you can pass url parameter as normal like ‘http://localhost/wordpress-4.9.6/wp-json/api/v1/getstuff?search=rdbms’

      I have tested with the following code

      add_action( ‘rest_api_init’, function () {
      register_rest_route( ‘api’, ‘/v1/getstuff’, array(
      ‘methods’ => ‘GET’,
      ‘callback’ => ‘myFunctionToGetStuff’,
      ));
      });

      function myFunctionToGetStuff( $request ) {

      return ‘hello world ‘ . $_GET[‘search’] ;
      }

      can you please refer this link https://developer.wordpress.org/reference/functions/register_rest_route/

Your comment:

Your Name

Comment:




Last articles