wordpress create REST Api

Example 1

//Api full url : http://localhost/wordpress/wp-json/api/v1/getstuff

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

function myFunctionToGetStuff( $request ) {
	return   'hello world'  ;
}

Example 2 – Passing parameter

//Api url http://localhost/wordpress/wp-json/api/v1/get/author/{number}

add_action( 'rest_api_init', function () {
    register_rest_route( 'api', '/v1/get/author/(?P<id>\d+)', array(
        'methods' => 'GET',
        'callback' => 'myFunctionToGetStuff',
    ));
});

function myFunctionToGetStuff( $request ) {
	return   $request['id']  ;
}

Example 3 – Passing multiple parameters

//url : http://localhost/wordpress/wp-json/api/v1/get/{parameter 1}/{parameter2}

add_action( 'rest_api_init', function () {
    register_rest_route( 'api', '/v1/get/(?P<param1>[a-z0-9\-]+)/(?P<param2>[a-z0-9\-]+)', array(
        'methods' => 'GET',
        'callback' => 'myFunctionToGetStuff',
    ));
});

function myFunctionToGetStuff( $request ) {
	
	return $request['param1']." ".$request['param2'];
}

Example 3- permission check

add_action( 'rest_api_init', function () {
    register_rest_route( 'api', '/v1/get/sitemap', array(
        'methods' => 'GET',
        'callback' => 'api_function_to_get_sitemap',
    	'permission_callback' => function () {
    		return permissions_check();
    	}
    ));
    
    register_rest_route( 'api', '/v1/get/page/(?P<page_slug>[a-z0-9\-]+)', array(
    		'methods' => 'GET',
    		'callback' => 'api_function_to_get_page_by_slug',
    		'permission_callback' => array( $this, 'permissions_check' )
    ));
    
    
});


function permissions_check(){
    //permission check will perform here
    
	return true;
}

 

$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/

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