DEV Community

Cover image for Introduction to WordPress REST API
GP Web Dev
GP Web Dev

Posted on

Introduction to WordPress REST API

The initial goal of the REST API in WordPress is to provide a way for others to interact with WordPress sites by sending and receiving data. The REST API in WordPress, if setting up the endpoints correctly, should be agnostic about what system is pinging the API. As a result, we can use WordPress as an application framework and build the client in whatever programming language we desire.

One main use of the WordPress REST API is the new block editor (Gutenberg). It’s useful to query and store data when creating custom blocks. As mentioned in the official documentation of WordPress, the REST API is language agnostic:

Any programming language which can make HTTP requests and interpret JSON can use the REST API to interact with WordPress, from PHP, Node.js, Go, and Java, to Swift, Kotlin, and beyond.

In simple words we can use any programming language to send data to an endpoint and expect a JSON response in which we can parse on the client-side. In the context of Ajax, we’re sending a payload to the REST API, and then parsing this data in JavaScript and perhaps displaying an output to the user.

WP-AJAX vs WP REST API

Since the introduction of the WordPress REST API, many plugin developers have started converting their plugins to use the REST API instead of the older AJAX API (admin-ajax.php). Aside from the REST API simply being newer technology, it is also faster and more reliable than the older endpoints due to the fact that not as much of WordPress is loaded during a typical REST request.

The REST API was merged in core more recently than Admin-AJAX. It is perfect to be used in mobile apps and API developments.

REST-API PROS

  • Simplicity. Simple to write, develop and debug
  • Does not need separate functions for logged-in and non-logged-in users
  • The core already has built-in handlers that speed up the development process
  • The response can easily be used in applications or platforms that do not run on WordPress

REST-API CONS

  • It does not produce any user-friendly response. The output is always in pure JSON. Some might find this an advantage
  • Working with JavaScript and JSON needs more knowledge than handling a simple text output

Admin AJAX existed in the core from the beginning and it is the way the core itself deals with the requests in the admin area.

Admin-AJAX PROS

  • It directly outputs the content (JSON, text, HTML, XML etc.) ready to be used anywhere.
  • It has separate functions for logged-in and logged-out users. While you can do this with a conditional in the REST-API, some may find this useful
  • Working with the response is easier, since you have already formatted it in your handler

Admin-AJAX CONS

  • Since the output is plain HTML (by default ) it shouldn’t be used in APIs and application development

They are both useful handlers and if any of them wasn’t secure, it certainly would not have existed in the core for so many years. It’s rather performance, type of request, and development platform that decides which one should be used.

From a performance standpoint, it’s clear that there is a slight advantage when using REST API. Adding custom API endpoints is incredibly easy, and since it doesn’t have to load as much of WordPress core (including the admin area and the commonly-used admin_init hook), it will likely be faster than using admin-ajax.php in most cases.

In terms of reliability, the REST API still depends on the quality and integrity of our code. A poorly coded plugin could still easily interfere with REST requests. Overall, it’s definitely a good idea to at least consider using the REST API. Adding custom API endpoints is incredibly easy, and it doesn’t take much to switch over existing code either.

Endpoints

In the WordPress REST API, routes are a list of available endpoints in the REST API. An endpoint is a final destination for the route. We can visit https://yoursite.com/wp-json/ to see the available routes and endpoints we already have in our website.

Some useful examples could be:

The REST API Handbook provides all necessary information on how to utilize it, along with frequently asked questions, usage examples and how to extend it.


Top comments (0)