Beginner's Guide To WordPress REST API

Beginner's Guide To REST API

Learn basics of WordPress REST API Interface, best tools to start with REST API development, generate custom endpoints and more

What is REST Interface?

API or application programming interface is a set of rules that define how can devices or applications can connect and communicate with each other. REST stands for representational state transfer and is a software architectural style created to guide design and development of services for modern World Wide Web.

To keep things simple, REST API is an unified way of working with data for modern web where frontend and backend are decoupled which gives much more flexibility when developing and designing applications.

The REST architecture is specifically designed for network-based applications where REST serves as a backend service so we can say it's client-server first architecture. Most of the popular internet websites and applications use REST for communications and is considered as a best way to communicate with servers.

REST vs Monolithic Architecture

Monolithic architecture approach is traditional architecture approach, still widely used, for example WordPress.
With monolith architecture type applications code is written as a single coherent type of code without ability to split architecture to frontend and backend. Once again, WordPress (by default) is best example of monolith approach where both frontend and backend are written in PHP and act like single application.

Advantages of monolith applications are: Ease of usage, Ease of development and performance benefits only in specific situations and cost.

Disadvantages of monolith applications are: Bloated/Complicated unless very small applications, bad scalability where one can usually scale only vertically and legacy problems if there is a need for adding newer technologies to monolith stack.

WordPress REST API

WordPress REST API is added to core WordPress in version 4.7 in 2016 year. Before that it was available as a WordPress plugin. Reason for adding WordPress REST interface is primarly to decouple frontend from backend, offering you new ways of building web applications with WordPress.

WordPress REST API supports most of the WordPress' functions out of the box giving you options to further develop quite easily. We can say that WP REST is a best way of developing if you need structured, simple and extensible way to manipulate web data.

How to actually start with WP REST API

REST API in WordPress is enabled by defauly, since version 4.7. To start, you basically append "/wp-json" to your website URL, for example:

Note: if you don't see JSON output when you visit that URL you should check WP Dashboard->Settings->Permalinks and activate permalinks which are needed to make API work.

API is currently in version 2 and default REST API routes available are:

  • /wp/v2/posts
  • /wp/v2/posts/[ID]
  • /wp/v2/posts/[ID]/revisions,
  • /wp/v2/posts/[ID]/revisions/[ID]
  • /wp/v2/posts/[ID]/autosaves,
  • /wp/v2/posts/[ID]/autosaves/[ID]
  • /wp/v2/pages
  • /wp/v2/pages/[ID]
  • /wp/v2/pages/[ID]/revisions,
  • /wp/v2/pages/[ID]/revisions/[ID]
  • /wp/v2/pages/[ID]/autosaves,
  • /wp/v2/pages/[ID]/autosaves/[ID]
  • /wp/v2/media
  • /wp/v2/media/[ID]
  • /wp/v2/blocks
  • /wp/v2/blocks/[ID]
  • /wp/v2/blocks/[ID]/autosaves
  • /wp/v2/blocks/[ID]/autosaves/[ID]
  • /wp/v2/types
  • /wp/v2/types/[ID]
  • /wp/v2/statuses
  • /wp/v2/statuses/[ID]
  • /wp/v2/taxonomies
  • /wp/v2/taxonomies/[ID]
  • /wp/v2/categories
  • /wp/v2/categories/[ID]
  • /wp/v2/tags
  • /wp/v2/tags/[ID]
  • /wp/v2/users
  • /wp/v2/users/[ID]
  • /wp/v2/users/me
  • /wp/v2/comments
  • /wp/v2/comments/[ID]
  • /wp/v2/search
  • /wp/v2/settings
  • /wp/v2/theme
Once again, to use it just append string to end of your site URL, for example:

Create new WordPress REST endpoint

While default REST endpoints provided by core WordPress are usable we usually need to create our own. Luckily, Brewpy allows us to quickly and easily create custom REST API endpoints

If you click that link you should see REST API generator page as seen on screenshot under:

Generate custom REST API Endpoint in WordPress
As you can see, there is a code already generated with a simple basic function for getting user data, let's explore generator options:
Theme slug - add your current theme slug name along with API version (currently /v2 is valid)
Endpoint function name with parameters - here you add name for API endpoint which will be used in URL together with regex (that's why we have that weird characters)
REST API function content - main part of endpoint, you should write your PHP function content here (without function declaration, just code)
For the sake of simplicity we will just change Theme slug option to accommodate our current WordPress theme we use, in our case it is twentytwentyone/v2.
Now copy code from right side of page without opening and ending PHP tags and add this code to bottom of your functions.php file.

If you did everything as described, your new custom endpoint should be live on[name] where [name] is string to search user for, for example: which should output all users with name John. JSON response should be something like this:

	"name":"John Doe",
More resources for WordPress REST API:


Working with WordPress REST API interface is easy and thank to Brewpy adding new endpoints are easy as breeze. In upcoming tutorials we will explore further and find out how to use REST API endpoints in real world example.
Make sure to read our tutorial on React JS vs WordPress we just published!