Blog

REST API in WordPress: When, Why, and How to Use It

Written by Tarhe on
Woman wearing pink cardigan sitting on round chain using laptop on lap

Think of how much of your business data you store in WordPress. What if you could access it and use it in different applications? Thankfully you can do that with the REST API in WordPress.

The REST API expands the power of WordPress beyond your website. Advanced developers can use the API to connect WordPress with other sites and applications.

This post is your introduction to the WordPress API and how you could use it for your site.

About APIs

Let’s start with some basics. What does API stand for? An API is an Application Programming Interface. An API allows two applications to communicate with one another.

APIs are the backbone of the modern internet. You probably use hundreds of APIs without realizing it. Companies often create APIs to encourage developers to utilize their services on their sites and applications. A common example is the Google Maps API which allows developers to embed maps with the full Google Maps functionality into their sites.

24/7 WordPress Support

from real WordPress experts

LEARN MORE

What is a REST API?

A Representational State Transfer, or REST, API complies with a set of standards that allow two different systems to work together. Without the REST protocols, the systems wouldn’t be compatible.

5 Essential Elements to make an API ‘RESTful’:

  1. Client-Server Architecture. The client application and server applications have to be separate to promote independent development. The server side is the platform the API belongs to, in this case, WordPress. The client is the application trying to connect to the platform. As the server-side technology evolves, the client-side application still needs to be able to access it using the same methods.
  2. Uniform Interface. Every part of the API needs to function using the same interface, language, and methods. For example, the URLs used to access resources must be consistent and uniform. 
  3. Stateless Protocol. REST APIs require the use of a stateless protocol. The client request provides all the data upfront, and the server response should include everything the client needs. Nothing about the request should be stored on the server. This approach reduces memory utilization and error potential. 
  4. Caching. A RESTful API uses cacheable data for better efficiency and speed. 
  5. Layered System. Developers build REST APIs using layers. The layers can interact, but they’re separate with their own functions. This layered approach makes it easier to update the API while also improving security.

The key takeaways to these RESTful protocols are that, by enabling a REST API, WordPress allows you to interface with your site without compromising security, privacy, or performance. The REST API also adopts a common internet standard making it easier for developers to access the API without learning something new.

About the WordPress REST API

The REST API in WordPress started as a plugin to add functionality to WordPress. The API was fully integrated into the core WordPress software with version 4.7 in 2016.

While originally intended and still widely used to enable separate applications to connect to your WordPress site, the REST API plays a key role in many recent WordPress advancements. The REST API in WordPress is the foundation of the new Block Editor. Many theme, plugin, and custom application developers are taking advantage of the REST API for managing and publishing WordPress content.

What Can I Do With the WordPress REST API?

The possibilities are endless with this API. Let’s look at some of the ways you could use the REST API.

Add External Data to Your Site

You can retrieve and add data to your WordPress site using the API. For example, a media company could pull updates from a Slack channel to create a live blog of all their reporters working on a big story, such as election night.

Use Your WordPress Content in an External Application

You also can use the API to display your WordPress data in another application. One of the most common uses of the REST API in WordPress is to pull out site content to use in a mobile app.

Create Easier Ways to Manage Your Site

The API also allows you to create new ways to manage your site. The most notable example is WordPress.com’s Calypso project.

The Calypso project is a reinvention of the WordPress Dashboard (WP-ADMIN). Calypso allows a user to centrally manage multiple WordPress sites and create content within a mobile-friendly user interface. It also loads significantly faster. You can read all about the story of Calypso. The creation of Calypso also allowed for the creation of a WordPress desktop app available on IOS/Windows/Linux.

Build New Interfaces for Editing Content

You also can use the API inside WordPress to create more intuitive interfaces for editing and managing content. For example, the API powers the new block editing features as well as many plugins. Understanding how the API works would be helpful if you want to create custom Gutenberg blocks.

For a taste of how the REST API changed the game with block editing, download our e-book on the basics of Full Site Editing. The e-book walks you through how to accomplish several common tasks only using blocks. This change in how to edit and manage WordPress sites wouldn’t be possible without the REST API.

Build New Front-End Presentations

The REST API enables creative and flexible ways to present content on your WordPress website. Theme developers may find it easier to rely on the API instead of PHP.

What Other APIs Does WordPress Offer?

The REST API is just one of many APIs WordPress provides. The REST API is the most robust and allows you the most flexibility in how you use it. The rest of the WordPress APIs are primarily used in plugin development, but they’re still an option to consider, depending on what you’re building.

Other WordPress APIs are:

  • Dashboard Widgets API
  • Database API
  • HTTP API
  • File Header API
  • Filesystem API
  • Metadata API
  • Options API
  • Plugin API
  • Quicktags API
  • Rewrite API
  • Settings API
  • Shortcode API
  • Theme Modification API
  • Theme Customization API
  • Transients API
  • Widgets API
  • XML-RPC WordPress API

You can view documentation on how to use each of these APIs on the WordPress Codex site.

Do I Need to Use the REST API in WordPress?

No, you don’t have to use the REST API, but you probably already are. The API is the basis for so many new features and plugins that you’re likely taking advantage of it on your site without realizing it.

As to whether you should start building your own applications or projects using the API, that’s a matter of personal preference and comfort level with the technology.

Use the REST API when you want to use a language other than PHP or are worried about performance issues.

Use the REST API When You Don’t Want to Use PHP

If you want to write a theme, plugin, or external application as a client-side JavaScript application or standalone program in any language besides PHP, use the WordPress REST API to access content with your WordPress site.

Use the REST API for Better Performance

Using the new WordPress REST API allows developers to create cutting-edge new features without using a ton of server-side resources. That means most features developed using the REST API will be faster than if developed using PHP. It also means you can deliver remarkable user experiences without adding expensive infrastructure.

What Do I Need to Know to Use the WordPress REST API?

To take advantage of the REST API in WordPress, you must be familiar with HTTP request methods, JavaScript, and JSON.

WordPress utilizes JSON or JavaScript Object Notation as the format for exchanging data in the REST API.

Most WordPress developers are familiar with PHP, but Matt Mullenweg stated at the WordCamp US 2015 State of the Word that it’s time for our ecosystem to learn JavaScript…. deeply. And we believe that the mass of developers in the WordPress community will take Matt up on this call. Not just because Matt suggested it but because of the remarkable experiences they will be able to put in front of their users. And you don’t have to take our word for it. There are enough compelling examples of the REST API in action today to encourage us all to step up to the challenge.

Why the REST AI and JavaScript Matter for WordPress Developers

With the JSON formatting, developers are no longer constricted to just PHP. The push to use the API in more aspects of WordPress development highlights the need to learn JavaScript. If this trend continues, developers may be able to rely on server-side JavaScript exclusively without using PHP.

Ideally, all of these changes will lead to faster and simpler development.

The Basics of the WordPress REST API

At the most basic level, an API processes a request that returns a response. The client (the app you built) asks the server (WordPress) to do something, and the API makes it happen.

Basic HTTP request methods used in the WordPress REST API are:

  • GET. Use this command to retrieve something from WordPress, like a particular piece of data. 
  • POST. This command adds a resource to WordPress, like creating a post. 
  • PUT. This command enables you to edit or update an existing resource on the server.
  • DELETE. Use this command to remove something from WordPress.

The actual API interactions are a bit more complicated and involve these basic elements.

Key Concepts of the WordPress REST API

Your API response cycle will include these basic elements:

  • Routes and Endpoints. Routes are URLs you enter to make a request. The endpoint combines a URL with an HTTP method. 
  • Requests. A submitted endpoint makes a request to the server. 
  • Responses. If you submitted a properly configured endpoint, you’ll receive a response with the data you requested in JSON format. 
  • Schema. The schema is the properties and input parameters the WordPress REST API accepts and returns.
  • Controller Classes. Think of the controller classes as the instructions for how you want the request to be handled. Controller classes manage and coordinate all the moving parts of an API response cycle.

For a more in-depth look at exactly how the WordPress REST API works and all the available options, read the REST API Handbook from WordPress’ Developer Resources.

Is the WordPress REST API Secure?

At first glance, you may wonder if anyone can access your site and start deleting posts via the API. Fortunately, you can require authentication for any API requests. The authentication ensures the user or application can make the requested changes.

The WordPress REST API Handbook puts it this way:

“The REST API is a developer-oriented feature of WordPress. It provides data access to the content of your site, and implements the same authentication restrictions — content that is public on your site is generally publicly accessible via the REST API, while private content, password-protected content, internal users, custom post types, and metadata is only available with authentication or if you specifically set it to be so.”

Optimal Site Performance With the REST API in WordPress and Pressable

We’re excited about what the REST API is doing for WordPress. It means there will be a lot of new features that will be faster and more sophisticated. It also means you can add more advanced, engaging, and immersive user experiences to a WordPress site, all without a major sacrifice to site performance.

At Pressable, we’ve dedicated a lot of resources and our engineers’ brainpower to ensure our clients never have to sacrifice site performance.

We utilize non-volatile memory express (NVMe) servers with improved response times, enhanced queuing, and optimal security. Your site runs faster and safer and will rank better in search engines.

The NVMe server infrastructure is about 10 times faster than the standard SSD technology used by most other hosting companies. You can trust our servers to respond quickly and scale effortlessly, no matter what you build or what method you built it with.

Sign up for a demo to learn more about why Pressable is the best choice for WordPress developers.

Web Performance

Load time matters! Do you know how fast your site is?

LEARN MORE

Tarhe

Obatarhe is a Customer Support Engineer at Pressable, WordPress Enthusiast, Community Volunteer, and Tech Advocate. He gets to do exciting things everyday, these include providing 5 star rated support to customers and rendering tailored service to resolve their issues. When he's not working, you can find him traveling and taking good landscape pictures.