What is REST API and How to Create REST API in WordPress

Posted By : Satyam Sharma | 25-Aug-2019
Introduction
 
In this blog, we will get to know about the REST API and how we can create the REST API in WordPress. So before know about the REST API, we must have knowledge about REST and API. So let's first understand these concepts.
 
REST is stands for the "REpresentational State Transfer". It is just a concept or an architecture for managing the information or the data over the internet. It is usually presented by the JSON formate and sometime it could be in XML format as well.
 
API stands for the "Application Programming Interface". These are the set of rules by which two or more than it, software and application communicate with each other. They can request data, send data, update data and delete data of each other by just following these rules. 
 
REST API is the lightest way to for communication between two software or application. If two applications can do create, read, update and delete kind of operation by simply hitting the URL with a request usually means they are using the REST API
 
 
REST API in WordPress
 
Although WordPress gives us the inbuild REST API by which we can perform create, update, delete and fetch operations, yet in some situation we need the own rest API. So before telling the custom rest API, we should have some knowledge about the WordPress rest API because this knowledge would be very helpful for a Wordpress developer.
Wordpress mainly use "/wp-json/wp/v2/" routes appended in your site domain for example if your site domain is "https://abc.com/", then your API for getting posts would be "https://example.com/wp-json/wp/v2/posts".
 
you can find more helpful information about WordPress REST API by click here.
 
 
How to create REST API in WordPress.
 
We can create a custom REST API as well. In WordPress, the best advantage for developers is the hooks. We can addon the functionality by using these hooks. We can also create custom rest API with the help of these hooks. 
For creating the REST API we have to follow the given steps.
 
Register the custom route
  There is a function in WordPress named "register_rest_route". With the help of this function, we can create our route. This function has the following parameters.
 
1. $namespace
2. $route
3. $args
4. $override
 
$namespace is a required parameter. In this, we have to give a first URI segment after the core prefix which must be unique.
 
$route is a required parameter here we have to give the base URL for your API.
 
$args is an optional parameter where we have to give an array specified the method and the end the callback function for the endpoint of API.
 
$override is also an optional parameter where we have to give a boolean value for giving the instruction either this route would override the previous one defined the same route or not. Its default value is false.
 
 
After register the route we have to initialize our route by using "rest_api_init" hook. Here below I have given an example showing how to create it.
 
add_action( 'rest_api_init', function () {
  register_rest_route( 'myroute/v1', '/author/(?P<id>\d+)', array(
    'methods' => 'GET',
    'callback' => 'my_route_func',
  ) );
} );
 
 
Here I have registered a route with giving a GET method API. So now our API would be https://abc.com/wp-json/myroute/v1/author/(?P\d+).
In this route, if you are able to see the last two name i.e  author/(?P\d+), we can also look it like author/{id}. here id is a dynamic value which we assign during hit this GET API. It is an integer value
 
 
Create an endpoint.
 
While we register our route we had given an array in the third argument in register_rest_route. In this array, we had given two key-value i.e methods and the callback. In the method, we had defined the request type of API and in the callback, we had given our callback function name and this callback function is our endpoint of API.
 
When we define this function we have to give it a parameter by which this function would get the value from the GET method API. for example here we had registered the API by assigned a {id} variable in the query string. So this value would get by the parameter which we are going to give in our function name. I think these are making conflicts in your mind so without wasting time let me give you an example.
 
function my_route_func( $data ) {
  $result = array();
 
$result['id'] = $data['id'];
  return json_encode($result);
}
 
So let suppose you have hit the API like "https://abc.com/wp-json/myroute/v1/author/1/", then you would get the result in JSON form like {"id":1}
 
Conclusion
 
After reading all these steps and method I hope you had got the basic fundamental about the custom rest API in WordPress. Here I am giving you just a small example for better understanding once you will understand about all aspects of this, you can create having lot bigger functionality endpoints REST API. 

About Author

Author Image
Satyam Sharma

Satyam Sharma is a skilled full-stack developer, who loves to get new opportunities and learning about new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..