WordPress API basics: What it means and how to use it

WordPress API basics: What it means and how to use it

You may have heard the buzz about the WordPress JSON REST API. This is big news for WordPress users and cause for celebration!

JSON, REST, API – this is a lot of jargon for one name, however, so let’s break it all down.

What is an API?

API stands for “application programming interface,” which is a set of programming instructions and standards for accessing a web-based software application or web tool. Imagine “software one” that is giving “software two” instructions. An API is an agreement between the two and the lines of communication are open. Basically, what they say to each other is something along the lines of “when you give me an instruction, I will perform the action, or return the correct information.”

As we search the web and use various software solutions, we are used to user interfaces with specific layouts, buttons, and graphics that we interact with to complete our desired tasks. All this is useless to a machine because all they care about is data and what they need to do and how they interact with it. An API is the software’s alternative to the “user interface,” giving instructions to a second software on how to get data and other specific tasks. They are completely invisible to users who are using the web application, and are simply a software-to-software interface.

wordpress-api-software-to-software

Examples of APIs

Having access to an API is awesome when you’re developing web applications. When an API is released to the public, it means the developers created solutions that are powered by the software’s service. Oftentimes, they take away the need to reinvent the wheel for many tasks, since they have already been created.

A great example of an API is Amazon. Amazon released its API so that developers can have easy access to the products and their information. Having access to this makes it easy to post links to products on amazon, with updated prices and the option to buy.

Another example of  APIs interacting would be concert ticket purchases. To officially purchase tickets, you need to enter your credit card information. An API is used to send your credit card information to a remote application, which then verifies that the credit card information is correct. Once everything checks out, the remote application sends a response back to the ticket website saying it’s okay to proceed, at which point you’ve successfully purchased the tickets. When all this is going on, you only see one interface, which is the ticket website. It’s a seamless process; behind the scenes, many applications are working together using APIs, but you as the user don’t notice when tasks are handed from one application to another.

API and WordPress

Amazon and ticket purchases are great examples of how APIs work together, but how does this apply to WordPress?
Well, all the same ideas apply. With the WordPress API, you are able to push and pull data out of WordPress.

The API is a key tool that developers can use to easily share data from their WordPress website to other websites or applications. Specifically, this API lets you perform create, read, update, and delete (CRUD) actions on WordPress content. A few examples of the content types are:

  • Posts
  • Custom post types
  • Pages
  • Taxonomies
  • Users
  • Media

What does REST mean?

There’s no napping or snoring here, but REST refers to the architectural style of an API. REST stands for Representational State Transfer. Remember how we mentioned that an API gives the specifics for communication so that programs can communicate with each other? REST API’s are designed in such a way that they provide a lightweight method of communication, which saves bandwidth. The WordPress JSON REST API does just this, making it a great option when there is a high volume of data.

We’ve just scratched the surface of REST; if you’d like to learn more, check out this REST helpful tutorial.

JSON format

To access data, we need a source to pull from. This is where JSON comes in. JSON stands for JavaScript Object Notation. JSON is easy for both humans and machines to read and understand. It’s the perfect way to store information in an organized, easy-to-access manner. Because JSON is so simple to access, this makes it a great fit for creating REST APIs and is the perfect solution for the WordPress JSON REST API.

Here’s a basic idea of what JSON format looks like:

{"employees":[
	{"firstName":"Jane", "lastName":"Smith"},
	{"firstName":"Anna", "lastName":"Olsen"},
	{"firstName":"Josh", "lastName":"Jones"}
]}

JSON format in WordPress

JSON doesn’t just write itself, but don’t worry, you don’t have to manually create the JSON file. There is a great plugin that creates it for you. With this tool, the site’s data will be in the correct format, including users, posts, pages, taxonomies, and more. The plugin can be found here. Also, there is some helpful documentation that will be valuable when you get started.

wordpress-api

Using the JSON REST API

Using the API is pretty self-explanatory; you’ll be grabbing your site’s data in simple JSON format. And if you have the plugin installed, you can play around with seeing your site’s posts in data form.

Retrieving or updating data is as simple as sending an HTTP request. HTTP is a request/response protocol. First, you send a request for data and then the data is is sent back as a response.

GET method

The GET method is used to retrieve information and has no effect on the data. Simply send a GET request to /wp-json/wp/v2/posts. Want to search for something? For example, if you’re searching “development” you’ll get the results with the GET request /wp-json/wp/v2/posts?search=development.

POST request

A POST request is used to send data. With the capabilities of the new API, for example, you could update a user. Let’s say you want to update the user with the ID of 10. All you have to do is send a POST request to /wp-json/wp/v2/users/10.

To try this out, I’d recommend using Postman, a Google Chrome extension.

More Javascript in the mix

Backbone.js and APIs

Backbone.js is a great way to work with the API because it is a framework designed for working with REST APIs. The WordPress API includes an easy-to-use Javascript API based on Backbone models. This allows plugin and theme developers to get up and running without super in-depth effort and knowledge about the details of getting connected. There’s a lot more to it, but Backbone gives structure to web applications by providing models, collections, and views, and connects it all to the WordPress API over a REST JSON interface.

wordpress-api-backbone

What does this all mean for WordPress developers?

With the availability of the WordPress API, new ways of utilizing it have increased and will continue to do so as it gains popularity. The WordPress API can improve just about any existing WordPress functionality that relies on Javascript. Here are a few examples:

  • form validation/submission
  • Backbone.js themes or plugins
  • infinite scrolling

It’s also a great way to show content from other sources. Now, it is easy to display content from an individual site within a multisite setup. It’s also possible to display content from an entirely separate WordPress site. In addition to showing content, the content can also be updated.

Now that we’ve gone over some of the basic terminology, we know that JSON is the notation, REST is the architectural style, and API is the interface they make up. Knowing the basics of the API and its capabilities will ensure you are off to a good start when you explore the new possibilities with the WordPress API.

Join the discussion

Get more great content in your inbox

More articles

Delightful managed WordPress hosting

The #1 choice of over 70,000 digital creatives

Tour the platform
[if lte IE 8]
[if lte IE 8]