Before we discuss the WP REST API, it's important to understand some terminology and background information. The acronym API stands for Application Programming Interface. An API is a programmatic way to interact with an application's data. For example, Facebook's API gives developers the ability to get all of the friends associated with a certain user. An API typically includes a specific set of instructions called documentation; to make it easy for any developer to work with it.
REST means Representational State Transfer. An API can be considered RESTful if its design/architecture subscribes to a specific set of constraints. You can look up what these constraints are here.
HTTP requests are often the way that you interact with a RESTful API (HTTP requests are also the primary way that data is transmitted across the Internet). HTTP means Hyper Text Transfer Protocol. This protocol allows information to be shared between a client (cell phone, tablet, laptop, desktop, etc.) and a web accessible server in a request-response protocol. As an example, in order to publish a status to a user's Facebook timeline, a HTTP request targeting that action on behalf of that user would be sent from our JavaScript code to Facebook's API (ie. a Facebook server). The client (JavaScript code) would receive a response from the Facebook server indicating that the user's status was successfully published.
1. A client makes a HTTP request to a server and 2. The server responds with an HTTP response.
In a HTTP request, you need to define the type of action that you want to perform against a resource. There are four primary actions associated with any HTTP request (commonly referred to as CRUD):
- POST (Create)
- GET (Retrieve)
- PUT (Update)
- DELETE (Delete)
A resource is a data object that can be accessed via a HTTP request. The WP REST API allows you to "access your WordPress site's data (resources) through an easy-to-use HTTP REST API". In the case of the most recent version of the WP API (version 2), the resources include the following 9 WordPress objects:
- Posts
- Pages
- Media
- Post meta
- Post revisions
- Comments
- Taxonomies
- Terms
- Users
With the WP API, you can perform any of the four CRUD actions against any of your WordPress site's resources listed above. For example, you can use the WP API to create a post, retrieve a post, update a post or delete a post associated with your WordPress website.
The post WP API – Using the WordPress REST API appeared first on SitePoint.