Best method for Adding an Rest API with WordPress coding!

In this article we are going to create an WordPress Rest API. Using this API we can integrate our data into Android/ios Mobile APP or various application. First we will talk about some basic concept and their features (Key Points) after that we will create an Rest API with different different method in wordpress.

What are the some key points of WordPress Rest API ?

  • Routes & Endpoints – route is URL where we can map it with http method like GET,POST etc. while an endpoint is a connection between an individual HTTP method and a route.
    Ex : – /wp-json is a route, and when that route receives a GET request then that request is handled by the endpoint which displays what is known as the index for the WordPress REST API.
  • Requests – Request is represented by an instance of the WP_REST_REQUEST class , using this class we can store and retrieve information for current request. It is automatically generated when we make HTTP request to a registered API route.
  • Responses – We are getting data from the API in Response. The WP_REST_RESPONSE class provides a way to interact with the response data returned by endpoints.
  • Schema – Schema’s are defined in data structured format. The schema structures API data and provides a comprehensive list of all of the properties the API can return and which input parameters it can accept.
  • Controller Classes – Using controller class we can manage the registration of routes & endpoints, handle requests, utilize schema, and generate API responses.

Creating an API by different Ways !

Method -1 – Using plugin , We can create an API.

  • Add WordPress REST API Basic Auth plugin.
  • Log in to your WordPress Dashboard and go to Plugins -> Add New. Click on the Upload Plugin button and select the plugin’s zip file or directly search Basic Auth from Search bar , you will get plugin name click on install button and after installation click on activate button to activate the plugin.
  • Once Basic Auth is installed, open CLI and authenticate an API request by utilizing the user flag.
    Ex : – curl -X GET –user username:password -i your curl url.

Method -2 – Without using plugin , We can create an API.

In this method you have to create one folder inside theme folder/child theme folder(if you are using) then you can create one more folder name called myapi or your choice name include this folder into functions.php file using below code.

require get_template_directory() . '/myapi/myfile.php';

Suppose if you want to create an post api then create one file inside myapi folder name and below code into this.

<?php
add_action( 'rest_api_init', function () {
  register_rest_route( 'gp', '/listpost', array(
    'methods' => 'GET',
    'callback' => 'get_blog_data',
  ) );
} );

function get_blog_data(){
global $post;
$args = array('post_type'=>'post','numberposts' => 5);
$myposts = get_posts( $args );
$dataArray=array();
foreach($myposts as $post){
$arr=array();
$arr['id']=$post->ID;
$arr['post_title']=$post->post_title;
$arr['post_content']=wp_trim_words($post->post_content);
$arr['post_date']=$post->post_date;
$arr['guid']=$post->guid;
$arr['thumbnail']=wp_get_attachment_image_src( $post->ID, 'full' );
$dataArray[]=$arr;
}

return $dataArray;
}
?>

Url should be – yourhostname/wp/wp-json/gp/listpost.

Note : – Method -1 has not tested but method has been tested and it is working properly.