WordPress REST API: What It Is and How to Get Started Using It

WordPress includes an exceptionally useful API called the HTTP API that can be used for sending data to and retrieving data from remote APIs. If you are building a plugin that talks to Stripe, MailChimp, or just about any other service that provides an API, you can use the WP HTTP API to make your job significantly easier. For this tutorial, I’m going to show you how to use wp_remote_get(), one of the several functions included in the HTTP API, to retrieve and parse JSON data from a remote API.

There are four primary functions that comprise the HTTP API in WordPress:

For this tutorial, we will use wp_remote_get() since we wish to retrieve data from an API and then parse it.

Most modern APIs return data in a format called JSON (Javascript Object Notation). While the name includes Javascript, you don’t actually have to write or even know Javascript in order to parse JSON. For all intents and purposes, JSON can be considered a mostly universal data format that just nearly all programming languages can read, with PHP being no exception.

A simple JSON object looks like this:


In order to use this in PHP, we need to translate it to a format that PHP understands. To do this, we can use the json_decode() function.

$json  = '{"key":"value"}';

// Translate into an object
$obj   = json_decode( $json );

// Translate into an array
$array = json_decode( $json, true );

It is really that simple, but this is also an exceptionally simple example.