yufiyo
Kateryna Ryzha
Content Creator
Updated on
Useful Resources

WordPress REST API Basics

What’s a secret ingredient for WordPress success? Easy. WordPress offers a simple and extensible way to create and edit content for all types of users. As the web becomes more dependent on JavaScript for the creation of dynamic interactive experiences, WordPress websites need new methods to interact with their users and developers. The new feature, called a REST API, makes it simple to connect WordPress with other sites and apps using JavaScript Object Notation (JSON) code for formatting. 

The WordPress REST API is a complicated subject. Still, owners of WordPress sites with little developing experience must be informed about its basics to know how to improve them.

In this post, I’ll talk about technical details and basic concepts of WordPress REST API, focusing mostly on its functionality and use cases.

What’s an API?

Let’s start by clarifying what an API is. Application Programming Interface is a set of codes that allows one program to communicate with another one through requests (also known as commands) and responses. 

When you use an app on your phone, the app connects to the Internet and sends requests to the server. It retrieves the necessary data there, interprets it, and sends it back to your app. This process is possible because of the Application Programming Interface.

There are many types of APIs that are used in different contexts for the creation of custom themes, apps, and extensions. The programs connected via API may be integrated into one system or be separate. WordPress already makes use of APIs for things like shortcodes, plugins, and settings to connect within the WordPress core layer. 

For some time, the word API described any kind of general interface that connected with the applications. In recent years APIs have become more standardized. They showcase stronger security, and they adhere to strict rules. In many cases, they are treated like products instead of codes. Modern APIs are accustomed to customers’ expectations and the environment where they are implemented. 

Now is the time to discuss what REST API is.

What’s a REST API?

Out of all the different types of APIs, it is one that adheres to the REST standards that are easily accessible and developer-friendly. 

To explain the importance of REST API for modern WordPress developers may help remind us of how data is shared online. To retrieve a piece of information from the server, I must send a request using the HTTP protocol. One example of this action is typing a URL command in the browser’s address line. Most often, it is a GET request (as in “get me data from this page”). Then the browser comes back with the response, providing the request was correctly written. 

Of course, web pages are interactive now, and I can make POST requests using page fields. This is done when I post articles and when I log in to my account. The response is different there, and a POST request may trigger action, like sending me to a different page. 

Developers build WordPress sites using this protocol as well as GET and POST (also PUT and DELETE) command. There’s no way around these basics, at least not now. So WordPress offers a new, improved way to interact with its bases via REST API.

The goal of REST (or RESTful) API is to set up a uniform architecture and set of protocols for responding to requests from sources external to WordPress. With the release of WordPress 4.7 in 2016, all the content on WordPress sites is accessible as raw data for external sources.

What Are the Five REST Principles?

For the API to have REST properties, it must adhere to five principles:

  1. Uniform interface. The URLs (or REST endpoints) used to access content must be uniform and accessible through common commands. 
  2. Stateless. REST API doesn’t store any information about the client on a server. Both request and response include all the necessary information for the interaction. This eliminates requirements for memory from the server and enhances security.
  3. Client-server architecture. The client application and the server application must be independent of each other. If server-side technology changes, the client must still be able to connect with the server using the same method.
  4. Layered system. REST APIs are built as multi-layered structures. The layers interact with each other but are separate. This makes it easier to modify the API.
  5. Cacheable. Temporary storage for specific data can be used on the client or server side. REST API stores data there to improve speed and efficiency.

In order for an API to be RESTful, it must adapt all of these principles. We will see later why they are crucial for REST API functionality.

What Are the REST API Commands?

As mentioned above, developers can manage WordPress data by running different commands. Here’s a list of WordPress REST API types of commands:

  1. GET. This is the most commonly used command. It retrieves a piece of information (such as a post or a page) or a list of all published items in the specified location. Having this data, it’s now possible to perform one of the following actions.
  2. POST. This command creates a draft post, page, or other empty resources that later can be filled using the next command.
  3. PUT. Running this command changes the existing post or resource. In order to get access to a post or resource, you must know its ID, which you can retrieve using the GET command.
  4. DELETE. It is used to delete the resource and its content. 

REST API uses JSON format to exchange data. It is a lightweight and human-readable format based on JavaScript. It interacts well with most programming languages, making it possible for developers to create WordPress applications on client-side JavaScript apps or command line tools. A typical JSON representation of a WordPress post looks like this:

[
  {
      "id": 157538,
      "date": "2017-07-21T10:30:34",
      "date_gmt": "2017-07-21T17:30:34",
      "guid": {
          "rendered": "https://www.sitepoint.com/?p=157538"
      },
      "modified": "2017-07-23T21:56:35",
      "modified_gmt": "2017-07-24T04:56:35",
      "slug": "why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it",
      "status": "publish",
      "type": "post",
      "link": "https://www.sitepoint.com/why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it/",
      "title": {
          "rendered": "Why the IoT Threatens Your WordPress Site (and How to Fix It)"
      },
      "content": {
         ...
      },
      "excerpt": {
          ...
      },
      "author": 72546,
      "featured_media": 157542,
      "comment_status": "open",
      "ping_status": "closed",
      "sticky": false,
      "template": "",
      "format": "standard",
      "meta": [],
      "categories": [
          6132
      ],
      "tags": [
          1798,
          6298
      ],

      }
  ]

What Are the Key Features of REST API?

A true REST API is fast, versatile, and flexible. Here are the key advantages of this API type:

  • Support all formats for data transfers. Other types of APIs are limited to specific file formats, and SOAP (Simple Object Access Protocol) only allows XML. REST API enables WordPress to connect with other websites regardless of the language they are written in, as long as they run on the HTTP protocol.
  • Thanks to JSON, REST is generally considered easier to work with, and it offers better support for browser clients. 
  • REST API offers higher speed and efficiency because of its cacheable characteristics. If the data is not dynamic, it can stay on the temporary “cache” storage from where it is retrieved faster.
  • Easy to learn compared to other types of data transfers. Developers working with REST API don’t have to learn HTML/PHP.

REST API’s functionality and ease of use can be enhanced drastically by using designated WordPress plugins. For example, the Crocoblock JetEngine has a REST API feature that allows creating tables with custom structures and managing them directly in the wp-admin panel. These tables can store and edit data using REST API URLs from external resources. 

There are some limitations to REST API because of the principles it’s built on:

  • Because REST deals with direct point-to-point interaction, it doesn’t work well in distributed enterprise environments.
  • Because of the REST stateless feature, in case of error, the user must retry running the command. There’s no built-in automatic fixing solution.

What Is WordPress REST API Used For? Examples and Use Cases

When building a WordPress site, there are a lot of possibilities for developers to improve the website by utilizing REST API’s capabilities. 

One particular task that REST API successfully accomplishes is providing internal communication between websites within one organization. Here’s a good illustration of such REST API implementation – there’s a central site and a few separate sites created to serve WordCamp events that take place in many locations. While the central site offers general information about the events, additional sites have info about local events and keep track of people who subscribed for local events.

Using REST API, we can set up a communication between these sites where event sites automatically send data about event attendees to the central site. This is how we can put up a joint email list or do centralized accounting.

Another reason why APIs are becoming popular is publicly available API keys made by big companies. For instance, Google developers can put parts of their Google Maps code together and make it available to the public. This way, you can take their API key and implement it to your WordPress site and enjoy Google Maps’ functionality. Such public API keys can be free or paid.

Finally, the REST API allows displaying WordPress content anywhere. It doesn’t even have to be a WordPress-driven website. All the amazing things that can be done using the WordPress plugin now can be enjoyed outside of the WordPress platform. I’m sure this will play a major role in the future of application development. Owners of WordPress websites can control what data is accessible to the public using authorization methods. 

It’s possible to connect a third-party application to the WordPress site and create, edit, or delete content from an app. So, if you have a store on a WordPress site, you can add items using your phone without even entering the website. 

FAQ

What is a REST API?

It is an Application Programming Interface that adheres to REST (or RESTful) principles.

What are the five guiding REST principles?

Representational State Transfer principles are uniform interface, statelessness, client-server architecture, layered system, and cacheability.

What is a JSON?

JSON (JavaScript Object Notation) is an open standard file format that RESTful web services use.

Do I need to install WordPress REST API plugins to run REST API?

WordPress autonomous REST API system is fully functional. Third-party REST API plugins are emerging to expand their functionality and enhance UI.

Is REST API the same as API?

REST API is a subset of an API that adheres to the constraints of REST architecture and interacts only via an HTTP protocol.

Conclusions

REST API became the most commonly used type of API. Developers continue finding more ways REST API can extend WordPress functionality. It is obvious that in the nearest future, its role as a WordPress core building instrument will increase. For now, it is important to know that the REST API allows connecting WordPress with external sources. 

WordPress REST API is a complex subject. However, at a basic level, its concepts are easy enough to grasp. Also, third-party developers create plugins to make the WordPress REST API interface user-friendly.