WordPress REST API

WordPress REST API, formerly known as the “WP-API” was released as part of WordPress Core in version 4.7, December 2016. This is changing the way we think about and work with data managed by WordPress. It separates the content of a site from its presentation in a new and more fundamental way. Before understanding what its means lets understand the concepts around this.

What are RESTful APIs and JSON?

REST is the acronym for representational state transfer. it is the software architecture of the web.

RESTful application is typically one that use the standard verbs GET, POST, PUT and DELETE to retrieve and send data from and to a remote server. It is using URIs pointing at resources (objects that contain data) to interface with external systems such as remote server, and use verbs like GET and PUT to perform operations on the resources on that server.

API is the acronym for application programming interface. A set of routines, protocols and tools for building applications and interfaces. When you use for example Google Maps or Facebook on your phone, you actually use an app that is using an API to get and display the data on your device.

Combining  REST and API, we get a RESTful API. RESTful API is an application programming interface that uses resources accessed through URIs to perform operations on data on a remote server through verbs like GET, PUT and DELETE.

JSON is the acronym for JavaScript object notation. JSON is what we get back from the request, JSON  is an open standard format used to transmit data objects in the form of attribute-value pairs for further processing. JSON is commonly used for asynchronous communication between browsers and servers.

The WordPress API is a RESTful API for WordPress that returns data objects in JSON format when provided with a URI pointing at a resource.

What is the WordPress REST API?

The WordPress REST API is a RESTful API that uses simple HTTP requests to access the data on a WordPress site in JSON format. It means that we can access the data that WordPress provides without having to go through a theme, RRS, or XML-RPC. When we say “access the data” we mean we can:

  • Create content in the database
  • Read content from the database
  • Update content in the database
  • Delete content in the database

Before we had the option to use WordPress REST API, we could access the data with a WordPress query in PHP, typically through a WordPress theme. The theme would use the WordPress templating engine and hierarchy to output the post, page or other content you requested through the address bar on your browser. In this situation there are some limitations, for example, if you wanted the content from your WordPress site to appear in a mobile app, you have to either build a custom PHP application to do it, or pass the data through antiquated protocols.

Using the WordPress REST API, we can bypass this entire process mentioned above, getting the data from a WordPress site using the API is as simple as submitting a standard HTTP request, or URL. once you have the data, you can use any application or programming language to do whatever you want with it. So now we are not limited to only using  PHP, now we can use JavaScript, Ruby, C#, ASP.net, Swift and Java to build an application that using WordPress data.

What are Routes and EndPoints?

Interacting with the WordPress API is done through accessing routes and endpoints.

EndPoint is a function available through the API in the form of verbs like GET, POST, PUT and DELETE. An endpoint performs a specific function by taking one or more parameters and returning the resulting data. 

Route is the “name” used to access the available endpoints. 

The Route is the URI and the endpoint is the action performed on the URI.

We can  get the data of a specific post with an ID 123 by using the get endpoint (GET) at the relevant route (in our case WordPress provide us the wp/v2/posts/123 route), we will retrieve the data from the database entry with ID 123 as an object. 

We can update the post with an ID 123 then we will use the put endpoint (PUT) with the relevant route, we will update the database entry for the object with ID 123 

We can update the post with an ID 123 by using the delete endpoint (DELETE) with the relevant route, we will delete the database entry for the object with ID 123.

WordPress REST API has a wide variety of routes and endpoints. by combining the routes and endpoint we can interact (making CRUD operations) with WordPress API without using the admin panel. 

Using the WordPress REST API

I have installed a WordPress website at the URL: http://nisan-sabag.com/nisan-rest-api.

To get all the data from the website I have to add to the url “/wp-json” : http://nisan-sabag.com/nisan-rest-api/wp-json. Typing it in the address bar of the browser will show you the result, a big JSON object data with all possible data that WordPress REST API can provide.

Typing this: http://nisan-sabag.com/nisan-rest-api/wp-json/wp/v2 will retrieve the data using the new improved REST API (v2 -version 2)

To get all existing posts in the database, we will add “posts” to the query: http://nisan-sabag.com/nisan-rest-api/wp-json/wp/v2/posts.

To get a specific post with an ID equal to 1, we will add “posts/1” to the query: http://nisan-sabag.com/nisan-rest-api/wp-json/wp/v2/posts/1

But we are not limited just to get data, we can delete, update and create posts, pages and any data model that we created in the database. But to do such things that will change the database we will have to use the username and password and for that we need to use JSON Web Tokens or OAuth. In the next article on the subject I will teach how to do that.

Learn more about REST API Handbook for WordPress at https://developer.wordpress.org/rest-api/.