Introduction

The WordPress REST API (Representational State Transfer Application Programming Interface) is a set of WordPress web service endpoints that provide you with an interface to interact with your WordPress website programmatically over HTTP requests. This API gives you the chance to interact with every entity (posts, pages, etc.) on your WordPress site. There are cases where one would prefer to interact with a WordPress site via the API. An example is when you wanted the advantage of the admin panel for creating content but wanted to be able to customize how that content is displayed by writing your own HTML, CSS, and JavaScript. For that, we can use the WordPress REST API.

A headless content management system (CMS) is a CMS that provides content management and storage capabilities but does not dictate how the content is displayed or delivered to end-users. We can make a WordPress website headless. Up until now, we’ve been using external APIs that we don’t control. But with WordPress, we can create our own API. All the content management happens on the WordPress admin panel, but displaying that content is handled on a separate website that we build using HTML, CSS, and JavaScript.

Understanding the WordPress REST API URL

Every REST API URL including WordPress REST API comprises a base URL, an endpoint, and a method. The base URL is the main part of the URL on which all other endpoints are based. An endpoint represents a function that can be executed against one of the resources in an API. It can be authenticated or unauthenticated. Public endpoints are unauthorized and don’t require user credentials, whereas private endpoints are authenticated and do require user credentials. Here is a typical API URL for the WordPress REST API:

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts
  • https://public-api.wordpress.com/rest/v1.1 is the base URL.
  • /sites/tutor563.wordpress.com/posts is the endpoint.

Depending on how you setup your Wordpress site, there are two options to use WordPress REST API. You can either use wordpress.org or wordpress.com. And each of these option has its endpoints.

The Methods

Each REST API endpoint is associated with a method that specifies the intended operation to be executed on the data within the resource. The primary operations that can be carried out on data through a REST API are create, read, update, and delete, collectively known by the acronym CRUD. Each of these operations is correlated with a specific HTTP method, as outlined below:

  • Create operation: POST
  • Read operation: GET
  • Update operation: PUT or PATCH
  • Delete operation: DELETE

You can try this endpoint https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts with a GET method in Postman(An API client) to see for yourself.

Query Parameters

Query parameters are appended to the end of a URL and are separated from the URL path by a question mark (?) and separated from each other by ampersands (&). Here’s the general structure of a URL with query parameters:

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts?order=ASC&pretty=true

WordPress.org

WordPress.org operates on a self-hosted setup, meaning you host it on your own server. The base URL for the REST API with a WordPress.org site corresponds to your own site’s URL and follows this pattern: [Your Site URL]/wp-json.

Some API endpoints for WordPress site built with wordpress.org are:

  • GET https://example.com/wp-json/wp/v2/posts - Query this endpoint to retrieve a collection of posts from the WordPress site hosted on https://example.com.
  • GET https://example.com/wp-json/wp/v2/posts/<id> - Query this endpoint to retrieve a specific post record from the WordPress site hosted on https://example.com. The id is the id of the post to be retrieved.

 WordPress.com

The WordPress REST API is also available for WordPress.com sites, making it possible to programmatically access and interact with content on WordPress.com-hosted websites. The base URL for WordPress.com REST API is the https://public-api.wordpress.com/rest/v1.1.

Some API endpoints for WordPress site built with wordpress.org are:

  • GET https://public-api.wordpress.com/rest/v1.1/sites/<$site>/posts/ - Query this endpoint to retrieve a collection of posts from the WordPress site hosted on https://example.com. The $site represent the Site ID or domain.
  • GET https://public-api.wordpress.com/rest/v1.1/sites/<$site>/posts/<$post_ID> - Query this endpoint to retrieve a specific post record from the WordPress site hosted on https://example.com. The post_ID is the id of the post to be retrieved.

Practical examples

Launch your API client or web browser and try the following examples:

Sorting

To sort the list of the data retrieving from wordpress site, you need to pass a query parameter called order with either ASC or DESC as it value.

For WordPress.com:

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts?order=ASC

API Sorting Image

For Wordpress.org

https://example.com/wp-json/wp/v2/posts?order=ASC

You can decide what parameter the sorting is based on by specifying the order_by query parameter and a field as its value. See example below:

For WordPress.com

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts?oder_by=id&order=DESC

For WordPress.org

https://example.com/wp-json/wp/v2/posts?order_by=id&order=DESC

Filtering

Filtering narrows down the data to meet specific criteria, making it easier to work with and display relevant information to the user. Filtering is a common operation in APIs, especially when dealing with large sets of data. Basically, it involves specifying one or more query parameters to retrieve a chunk of data from sets of data. See an example below:

For WordPress.com:

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts?fields=ID,title&pretty=true

API Filtering Image

For WordPress.org:

https://example.com/wp-json/wp/v2/posts?fields=ID,title&pretty=true

In the URL example, the field parameter lists the properties to return per record from the WordPress site while the pretty parameter ensures the data returns in JSON format. You can try this in your browser.

Pagination

You can use the page parameter for pagination. The page returns the Nth 1-indexed page of posts. See example below:

For WordPress.com:

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts?page=1

API Pagination Image

For WordPress.org:

https://example.com/wp-json/wp/v2/posts?page=1

Embeds

To access related resources for an item, add the _embed parameter to your URL. For example, if you set featured images on your posts, you can use /wp/v2/posts?_embed to retrieve featured images added to the API call.

For WordPress.com:

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts?_embed=author

API Embed Image

For WordPress.org:

https://example.com/wp-json/wp/v2/posts?_embed=true

To execute a search query, you’ll need to append a search parameter to your API request URL. See examples below:

For WordPress.com:

https://public-api.wordpress.com/rest/v1.1/sites/tutor563.wordpress.com/posts?search=php

API Search Image

For WordPress.org:

https://example.com/wp-json/wp/v2/posts?search=php

API Documentation

As with all APIs, the best way to know how to use and work with them is to read the API documentation. Make sure that you’re using the resources that the API developers have given you.

READ

As you can see, working with WordPress’s REST API is quite simple. If you want more information about using WordPress’s REST API, you can watch the below video.

WATCH

Video: WordPress: REST API (2h 7m)

WooCommerce API

WooCommerce extends the WordPress REST API to include routes for products, orders, customers, and more. You can find the documentation for the WooCommerce API here.

Please be aware that as an ecommerce package, WooCommerce requires stricter security than WordPress. You will need to use authentication to access parts of the WooCommerce API. In order to view products without using authentication you will need to access this endpoint: /wc/store/products.

Activities

Activity 1

WATCH

The below video: https://vimeo.com/546521086/e7b75810eb


Lesson Task

Brief

Using the WordPress REST API, create a separate, standalone webpage (not built as a page on WordPress) that lists the products you created with WooCommerce on your WordPress site in Module Assignment 1.

Because you’re using the API, each time an administrator creates or updates a product, the change will pull through to your product list page.

Your product page should be made using HTML, CSS and JavaScript and include a list of all the product names, images and prices.

Level 1 Process

  1. Create the page by fetching data from the WordPress REST API on your web host and displaying it on the page.
  2. Create a new product on your WordPress admin page, and make sure it pulls through to your products page.

WATCH

Attempt to work with the WordPress REST API first, but if you are confused or would like more info on the WordPress REST API, please watch this vimeo video: WordPress REST API

References

Tags: