Book Image

Learning WordPress REST API

By : Sufyan bin Uzayr, Mathew Rooney
Book Image

Learning WordPress REST API

By: Sufyan bin Uzayr, Mathew Rooney

Overview of this book

The WordPress REST API is a recent innovation that has the potential to unlock several new opportunities for WordPress developers. It can help you integrate with technologies outside of WordPress, as well as offer great flexibility when developing themes and plugins for WordPress. As such, the REST API can make developers’ lives easier. The book begins by covering the basics of the REST API and how it can be used along with WordPress. Learn how the REST API interacts with WordPress, allowing you to copy posts and modify post metadata. Move on to get an understanding of taxonomies and user roles are in WordPress and how to use them with the WordPress REST API. Next, find out how to edit and process forms with AJAX and how to create custom routes and functions. You will create a fully-functional single page web app using a WordPress site and the REST API. Lastly, you will see how to deal with the REST API in future versions and will use it to interact it with third-party services. By the end of the book, you will be able to work with the WordPress REST API to build web applications.
Table of Contents (16 chapters)
Learning WordPress REST API
About the Authors
About the Reviewer

Introducing REST API

Before going any further, we first need to be aware of what REST API is, why it is called so, and so on. However, let us first try to understand the concept in a nontechnical manner and then delve into the technical details.

Defining API

Since we are capitalizing the term REST API, it is obvious that it is just an acronym. The three letters API stand for application programming interface.

In simple words, an application programming interface lets you establish a connection or link between two different types of software. For instance, your computer has a USB port, which is essentially meant for connecting USB storage devices such as flash drives or USB hard disks. However, you can connect virtually any type of USB hardware to the port-printers, smartphones, tablets, and so on. As such, think of the USB port as an API for letting you connect different types of devices to your computer and allowing your computer to interact with the concerned devices accordingly. Much like a USB port facilitates the exchange of data between two physical devices, an API facilitates the exchange of data between two different types of software.

APIs have been around for quite sometime and developers and programmers use them on a daily basis. Have you ever used a third-party app to post to your social networking feed? Say, using a plugin in WordPress to tweet about your new blog post as and when you publish it? Yes, that is possible by means of API. In fact, many games and apps that rely on social logins via Facebook or Google accounts use APIs to interact with the concerned social networking services.

Therefore, the lesson here is that APIs allow developers to use content and features from a different application, service, or platform in a service, platform, or application of their own, in a secure and limited manner.

Defining REST

Much like API, REST is also an acronym, and it is sometimes written as ReST. It stands for Representational State Transfer and refers to a given style of API-building. Almost all the major web services, such as Google, Facebook, and Twitter, rely on REST for their APIs simply because REST is based on HTTP (which happens to be the protocol that powers nearly all of the Internet connections). Plus, REST is lightweight and flexible and can handle large volumes of activity with ease.

Therefore, REST in itself is not a new trend and has been used on the web to power services for quite a long time. Thus, for WordPress users, harnessing the power of REST API means your applications can interact with a load of services right from within WordPress, with the help of REST API.

Thus, REST is an architectural paradigm for web services, and services that use such an architectural paradigm are known as RESTful services.

The underlying idea behind REST is that instead of relying on complex web services such as SOAP or XML-RPC, a simple HTTP protocol is used for making connections. Therefore, all RESTful applications make use of HTTP requests for handling all four CRUD operations, namely create, read, update, and delete. This makes REST extremely versatile, and anyone can roll out their own version of REST with standard library features using the programming language of their choice, such as Perl or PHP.

Even more, REST is fully platform-independent, so you can use it in scenarios where the server might be Linux but the client can be using Windows and so on. Since it is standard-based and language-independent, a RESTful request carries with it all the information that might be needed for its execution or completion.

However, such simplicity and versatility does not mean that RESTful applications are weak in any regard. REST is powerful and can handle virtually every genre of action or request that might be expected from any of its counterparts.

Lastly, it is worth noting that much like the other web services such as SOAP or RPC, REST too does not offer encryption or session management features of its own. However, you can build such features on top of HTTP within minutes. For example, for security, you can rely on usernames/passwords and authentication tokens, whereas for encryption, REST can be used on top of HTTPS (secure HTTP). RESTful applications can function in the presence of firewalls as well.

Speaking of RESTful applications, what are some of the most common uses of REST in practice?

Well, Twitter has had a REST API since the very beginning, and for all practical purposes, it is still the most common API, being used by developers creating apps and tools that work with Twitter. You can learn more about it at

Similarly, Amazon's S3 Cloud storage solution too relies on REST API; for more information, refer to

Flickr's API for external developers supports REST integration as well; for more information, refer to

And finally, the Atom feed services, an alternative to the otherwise more popular RSS, is RESTful in its nature.

We will come back to REST later in this chapter, but first, let us familiarize ourselves with another important term, JSON.

Defining JSON

JSON is an acronym for JavaScript Object Notation. As the name suggests, it is a form of data exchange format that is based on JavaScript. With more and more JavaScript libraries and services coming up, JSON is rising in popularity on the web.

The best part about JSON is that it is both machine and human-friendly in terms of reading and comprehension. As a developer, you can read it and write it as much as you would work with any other programming language, whereas computers can easily parse and process it too. In fact, many popular programming languages offer their own interpreters that can parse the output to JSON and back. This makes JSON ideal for cross-platform interaction application A coded in one programming language and application B coded in another programming language can interact by converting their data structures into JSON and back, and so on.

This feature of JSON has made it a universal connector on the web. For WordPress users, JSON can also be used to replace the nearly outdated XML-RPC standard (more on this in detail in a subsequent chapter of this book).

Now that we are aware of what the terms API, REST, and JSON stand for, let us come back to REST API and start by first learning more about the REST API in itself. Thereafter, we will focus on what it can do for WordPress developers and then get started with is usage in WordPress.

So, what can REST API do or, in other words, how has it been proving to be useful?