Before we delve into how to use the WordPress Rest API, let us define what REST API is. The acronym API stands for Application Programming Interface. An API is simply a programmatic way that enables you to interact with any application data. A fitting example would be Facebook’s API allows the programmers to get all friends associated with a particular user. In a nutshell, API’s contain specific set of instructions that make it easy for any developer to work with.

A Guide to Using WP REST API

REST means Representational State Transfer. A particular API can be said to be RESTful if its design or architecture adheres to certain constraints.

HTTP (Hyper Text Transfer Protocol) is often the way one would interact with RESTful API. Basically, HTTP is simply the way data is transferred over the web. What this means is that this protocol allows information to be sent to a web accessible server from a client.

HTTP request need to be exclusively defined by the type of operation requested. There are four actions that are associated with this protocol:

  1. POST
  2. GET
  3. PUT
  4. DELETE

Also known as CRUD.

So with the definitions and mode of operation out of the way, we can get down to how to get you started with our WordPress REST API tutorial.

Getting Started With the WP REST API

So first things first, you need to have the REST API plugin to get started and of course the latest version of WordPress. I will be using a simple project to show you how to get around so let’s start.

First, let’s create a local WordPress installation which will pull posts from a live site using the REST API and also, ensure that you have the REST API installed on your live site. In case you have no idea of how to do this, worry not. Here is a code to get you through.


class My_Author_List_Widget extends WP_Widget {
public function __construct() {
$widget_details = array(
'classname' => 'rest-api-test-widget',
'description' => 'A REST API test widget that pulls posts from a different website'
);
parent::__construct( 'rest-api-test-widget', 'REST API Test Widget', $widget_details );
}

public function form( $instance ) {

$title = ( !empty( $instance['title'] ) ) ? $instance['title'] : '';

?>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>">Title: </label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php

}
public function widget( $args, $instance ) {
echo $args['before_widget'];
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}

// Main Widget Code Here

echo $args['after_widget'];
}
}
add_action( 'widgets_init', function(){
register_widget( 'My_Author_List_Widget' );
});

Grabbing Some Posts

A Guide to Using WP REST API

To make a request, we need some things straightened out:

  • The base path of the API
  • The route used
  • The endpoint used
  • Headers required
  • Parameters

The base path would be /wp-json/wp/v2/. The complete URL would be http://mywebsite.com/wp-json/wp/v2/posts/.

Each route however, can have a various endpoints specified by the HTTP method. Our route has three endpoints:

  • GET that retrieves the post
  • PUT that updates the post
  • DELETE that will erase the post

Our example uses the http://mywebsite.com/wp-json/wp/v2/posts/ route with a GET endpoint that will fetch posts. Below is a simple line of code.


$response = wp_remote_get( 'http://mysite.com/wp-json/wp/v2/posts/' );

The data returned from the code above can grabbed by using the wp_remote_retrieve_body() function. The code for that is as follows.

public function widget( $args, $instance ) {        $response = wp_remote_get( 'http://mysite.com/wp-json/wp/v2/posts/' );         if( is_wp_error( $response ) ) {               return;        }         $posts = json_decode( wp_remote_retrieve_body( $response ) );         if( empty( $posts ) ) {               return;        }           echo $args['before_widget'];         if( !empty( $instance['title'] ) ) {               echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];        }          if( !empty( $posts ) ) {               echo '<ul>';               foreach( $posts as $post ) {                       echo '<li><a href="' . $post->link. '">' . $post->title->rendered . '</a></li>';               }               echo '</ul>';        }         echo $args['after_widget']; }

In this WordPress REST API tutorial, we are simply scratching the surface because there is way too much to be explored in the REST API. Replacing HTTP API functions with say something like cURL could open doors to more exciting stuff.

Some other things that you could do with REST API include caching responses, authentication and so much more.

 


What’s Next?

You can click here to get in touch with us and get professional help if you do not want to bother yourself too much with this process.

Our team of experts is on standby 24/7 to help you in case you need it.