WordPress REST API 101

WP-REST API

I have recently been exploring the REST API plugin for WordPress and how it works, and why it is such a big deal, as I’ve been hearing for a while that the WP Rest API is “a game changer” in the web development world. Of course, as a web developer, we hear this every month about some new technology, so I was a bit skeptical.

The nuts and bolts

The WordPress REST API is now in Version 2 and was introduced a few years ago. It can be installed and activated as a plugin simply named WP REST API. “REST” is an acronym and stands for “REpresenatational State Transfer”. “API” is also an acronym standing for “Application Program Interface”. There is one more acronym in this equation and that is “JSON” or “JavaScript Object Notation”.

The WordPress REST API provides API endpoints for WordPress data types that allow developers to interact with sites remotely by sending and receiving JSON objects. When you send a request to the API and receive a response this data is returned in JSON format. In short, this allows client side interfacing with API’s using JavaScript and CRUD operations. “CRUD” is yet another acronym and stands for “Create, Read, Update and Delete”. Any programming language that can make HTTP (Hypertext Transfer Protocol) requests and interpret JSON can interact with WordPress through the REST API. This means that Single Page Applications (SPA’s) can now be created on top of WordPress, and other applications can be created using WordPress as the JSON endpoint engine.

HOW IT WORKS

Once the WP-API plugin is installed in your WordPress site if you type in yourblogURL followed by /wp-json/wp/v2/posts you will see all of the posts on your site formatted as JSON. You can also delimit the response by typing in ?per_page=2 on the end for example. Additionally, you can also display post with a certain category by adding on the parameter ?categories=2. Not only can you do this for posts. You can also do the same for pages, authors, media, users, taxonomy, tags, comments and any other built in WordPress endpoints. You can also use PHP to create your own custom JSON endpoints.

With these JSON endpoints, you can create JavaScript code to GET, POST, PUT, and DELETE the data to fit your needs. With a little AJAX (Asynchronous JavaScript and XML) you can GET, POST, PUT, DELETE data without a page refresh.

I am fairly new to the WP API and I am experimenting with ways I can leverage the power of the WP-API via turorials and my own experimenting. I found a cool plugin in the WordPress plugin directory called SearchWP Live Ajax Search which makes use of the WP API to retrieve an AJAX search feature that I am currently using on this blog.

I’m curious as to how other companies and individuals are using the WP-API (if you are). WordPress started out as just a blogging platform but has evolved into an extendable web platform where you can develop fully blown websites and applications.

Feel free to comment and let me know of creative ways to use this powerful feature in WordPress.

WP REST API resources

Read More

Leave a Reply