How to use REST WordPress API

Sometimes using the REST API without proper programming skills can be complex but it opens several opportunities for WordPress to grow and add new functionalities.

Updated: 01 Jun, 23 by Susith Nonis 9 Min

List of content you will read in this article:

WordPress has its benefits but everything needs to be evolved. So making the required changes to WordPress’s features and functionalities, you can use the REST API. Using the REST API will make a potential difference to the future of WordPress from the codebase, users, developers, and its usage perspective.

It allows other systems to understand and interact with WordPress. Using the REST API, other systems will be able to access the WordPress database, access the data, and add data to it. It was specially designed in response to the change of the development way of websites and apps.

WordPress REST API works as an interface for various applications that will interact with the WordPress site. The interaction is done by sending and receiving data as JSON objects. It is the base for the WordPress Block Editor that allows you to enable the theme, plugin, and customizing the applications for managing your website effectively and efficiently.

The REST API allows you to create plugins that provide an entirely new user experience. 

In this article, we will learn about the WordPress REST API, how to access it, and some real-time examples where REST API is used.

Both REST and API working together provides you with a way for modelling and accessing the data of your application as objects and collections. With WordPress, you will get REST endpoints that will represent the posts, pages, and other built-in data types. These endpoints can receive or send JSON data to your applications for performing tasks on your content. Whenever you send or receive the content request to the API, you will get the response in JSON as it is widely supported in several programming languages. 

In December 2016, the WordPress REST API was released as a part of the core in version 4.7. But before that, the REST API was available as a plugin. It helps in supporting various applications that are built on WordPress and help in transforming WordPress from being a CMS to an application platform. 

It is commonly used by WordPress.com where a JavaScript-based interface works on the REST API for interacting with the database of WordPress. You can even use it with the Gutenberg editing interface. With the help of REST API, various applications are now able to use WordPress.

However, you can easily run complex websites using CMS, but you can use an application platform for powering web-based single-page applications. Using this, the content will get refreshed whenever the user takes any action rather than loading new pages. It is because it works on JavaScript rather than using PHP. This will help in carrying out various tasks without sending the request to the server every time.

Whether you are a user or WordPress developer, if you are working on any website then the REST API will have several implications. Below you can find some of the implications for WordPress users and developers.

As a user, you will have the below-mentioned implications.

  • It helps in making changes to the interface along with the Gutenberg editor.
  • It allows you to make changes and improve the WordPress mobile applications.
  • You will also get a self-hosted WordPress admin screen similar to WordPress.com.

As a developer, there are some below-mentioned implications

  • You will be able to create several SPAs for WordPress using REST API.
  • You will be able to integrate various frontend technologies with WordPress.
  • You can even create new functionalities with WordPress without writing code in PHP.
  • If you are a PHP developer, then it is a necessity to expand your skillset to JavaScript for improvement.
  • You will be able to make various changes to the Gutenberg blocks rather than meta boxes within pages and posts.

It is evident that there will be more contributions to the WordPress core using codes in JavaScript but not PHP. Over time, learning JavaScript for improving the functionality of WordPress is necessary, allowing you to make more dynamic changes to the website.

If you want to access the WP-REST API of your WordPress, you need access to the command line for accessing your website. For WordPress, you will get WP-CLI. You cannot make such changes using the admin dashboard or by changing the code of your site. We have mentioned some of the ways for accessing the WordPress REST API.

Using WP-CLI

Using WordPress’s command line “WP-CLI'', you will be able to work with WordPress. Some of the web hosting or WordPress hosting provides WP-CLI as a pre-installed tool. For accessing the CLI, you can open a terminal on your Mac or Linux system or a command prompt in Windows.

If you want to access the remote site, then you need the SSH login details to the server and access it via WP-CLI. If you are looking for connecting to the local site, you can use the command line for using the correct directory structure. It will be a smart move for testing the REST API on the local site before you use it on the live site. 

Authentication

After accessing the website, you need to go through the authentication. But for some endpoints, you will not require authentication as they are public. If you are using the REST API, then you do not have to log into the site admin. If you still want to authenticate your website via WP-CLI, then you have to install a plugin for authentication. If you are running any development, then you can simply use the Auth plugin and start your work. 

But, if you want an authentication plugin for your live sites, then you require a more secure authentication plugin like JWT Authentication PLugin that uses the JSON web tokens to ensure effective security. After that, you can use the command line commands for accessing the data along with authentication. 

You can run the below curl command for testing the connection to WordPress and you will get an output with a list of draft posts.

curl -X GET --user user_name:pass_word -i http://your_site.com/wp-json/wp/v2/posts?status=draft

You need authentication for accessing the draft posts as they are not public information. But for accessing the public data, you will not require authentication. So, for retrieving the published posts that are public, you can use the below URL.

curl -X GET http://yoursite.com/wp-json/wp/v2/posts

With the REST API, you will be able to change the future of WordPress in terms of themes, plugins, and other functionalities. We have mentioned some of the real-life examples that are using the REST API for creating SPAs or for linking WordPress to other websites and technologies.

WordPress.com

The WordPress.com admin screens have used the REST API for providing the SPA that helps the users to interact and manage their website. It helps in maintaining the dynamic communication between the interface and the server, resulting in a more advanced and interactive interface similar to WordPress.org.

The Block Editor (Gutenberg)

Earlier, the Gutenberg editor in the WordPress admin made use of the REST API for communicating with your database and creating the block. But now, you can also use the Gutenberg editor in the self-hosted WordPress also.

For using the post types along with the Gutenberg editor, you must use the REST API and make it enabled. It means for registering a custom post type with the help of Gutenberg, you should add the following code for enabling the block editor for that post type:

"show_in_rest" = true;

Event Espresso

You can use the Event Espresso plugin available in your WordPress. It helps the users in organizing and publicizing the events. It relies on the REST API, allowing the users to access the data from outside of WordPress. It means you are able to build mobile apps or SPAs for managing your events.

UsTwo

UsTwo agency whose website is created using the REST API as a single page application. It has a front-end using React and the backend supported by WordPress. Their page content is modular and has a structure different from the standard WordPress page. For this, they have used the custom page builder plugin for creating the modular content of the site.

Sometimes using the REST API without proper programming skills can be complex but it opens several opportunities for WordPress to grow and add new functionalities. For evolving WordPress, it is important that you use the REST API to effectively improve the future of WordPress. In this article, we have mentioned what REST API for WordPress, what implications it has and various real-life applications.

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.

user monovm

Christy Crawford

2021, Nov, 21

Your style is so unique compared to other people I have read stuff from. Thank you for posting when you’ve got the opportunity, Guess I will just bookmark this site.

user monovm

Lucy Glover

2021, Nov, 21

very nice and informative article keep up the good work