What is the WordPress REST API

What is the WordPress REST API

John Hewick

This month we’re going to be talking a lot about the WordPress REST API.

But what in tarnation is the WordPress API?!

The WordPress API is the WordPress Application Programming Interface. It can be split off into a number of API sections and topics. These different topics handle a different element of the functionality of the WordPress interface. There are APIs for dashboard widgets, your database, metadata, plugins shortcode, all parts and aspects of the WordPress installation to ensure that all parts can function effectively with all the others.

The newest API is the REST API. There has been a fair amount of chatter in the world of WordPress developers about the upcoming release. People are saying it’s the biggest thing to happen to WordPress since the release of the custom posts update way back in the 2.9 release of the CMS. Frankly, I don’t think that’s ambitious enough. The REST API is huge news.

The REST API will allow developers to completely separate their build from the standard WordPress front end. This means that we can expect mobile apps, super customisable themes and lots of clever little things we can’t even imagine thought up by some of the world’s leading WordPress developers.

Getting Started

To get yourself up to date with the REST API, you’ll need the latest version of WordPress and the REST API.

As a way of getting to know the REST API, here’s a brief tutorial which will create a local WordPress installation that pulls posts from a live site using the REST API. Install the API on the site and empty widget plugin on the local site.

/**

* Plugin Name: REST API Test Widget

* Plugin URI: http://danielpataki.com

* Description: This plugin adds a widget that pulls posts through the REST API

* Version: 1.0.0

* Author: Daniel Pataki

* Author URI: http://danielpataki.com

* License: GPL2

*/

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’ );

});

You can find this code within a rest-api-test-widget folder in the plugins directory. It is in a file named rest-api-test-widget.php. This contains the plugin header and all the stuff you need to make a widget.

The WordPress API will also come in handy here for requests and read responses.

To get the posts, you’ll need:

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

So the base path is always going to be /wp-json/wp/v2/ – with the base URL http://mywebsite.com/wp-json/wp/v2/. The route is /posts, with the full route URL http://mywebsite.com/wp-json/wp/v2/posts/. Just to make things trickier every route can have a different end point which is why you need to know what yours is. The endpoint is differentiated using the HTTP method. The route may be /posts/342. The /posts route has three endpoints:

  • GET will retrieve the post
  • PUT will update the post
  • DELETE will delete the post

We’ll use the GET endpoint to retrieve the posts using the http://mywebsite.com/wp-json/wp/v2/posts/ route. With the HTTP API, it’s one line of code:

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

Success is found when the posts are retrieved and we don’t return a WP_Error. The response contains the data we need which can be extracted using the wp_remote_retrieve_body() function.

The code we need for displaying the JSON encoded string with post data in our widget is:

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’];

}

While I know this is a little simple, the potential for the REST API is enormous. The code can be WordPress-agnostic if we replace the HTTP API functions with something like cURL. This means you can use it for a mobile app for your e-commerce store, one which can be used with both Android and iOS which can take orders, arrange shipments and receive payments without the need to visit the website. WordPress is now also an app platform.

The future is here, ladies and gentlemen.

John Hewick

Author John Hewick

More posts by John Hewick