REST API is an API that follows a set of rules for an application and services to communicate with each other. As it is constrained to REST architecture, REST API is referred to as RESTful API. REST APIs provide a way of accessing web services in a flexible way without massive processing capabilities. WordPress REST API is used in accessing your WordPress data without the use of the actual tool.

In this blog, you will understand the complete working of WordPress REST API. You will be introduced to WordPress and WordPress REST API. The commands that are involved in working with WordPress REST API will be elaborated with examples.

What is WordPress?

wordpress logo

WordPress is an Open-Source platform that makes website building simple, publishes content, and administers it. WordPress.com is a blogging platform, first and foremost. While it may be used for simple WordPress websites without a blog, it was built as a blog Content Management System (CMS); therefore, it has a lot of functionality tailored to that use case.

WordPress REST API gives you an option to access WordPress without the actual user interface. WordPress REST API is mostly used by developers to use WordPress without installing the WordPress tool. WordPress REST API enables the platform to interact and migrate data with any website or application despite the language that the platform uses. Using this WordPress REST API, you can send and receive data as JSON objects. WordPress REST API creates a new admin experience for WordPress using plugins. You can build interesting front-end user interfaces and have your WordPress content in a different application.

With WordPress REST API, you can have data access to all your WordPress content. This WordPress REST API has the same authentication restrictions when accessing public and private content. One of the most important attributes of WordPress REST API is that it allows the block editor and modern plugin interfaces without disturbing the security or privacy of your website.

Simplify REST API Connection with Hevo!

Ditch the manual process of writing long commands to connect your REST API and choose Hevo’s no-code platform to streamline your data migration. 

With Hevo:

  1. Easily migrate different data types like CSV, JSON etc. 
  2. 150+ connectors like REST API(including 60+ free sources).
  3. Eliminate the need of manual schema mapping with the auto-mapping feature.

Experience Hevo and see why 2000+ data professionals including customers, such as Thoughtspot, Postman, and many more, have rated us 4.3/5 on G2.

Get Started with Hevo for Free

Working with WordPress REST API

To get started with WordPress REST API, you must access your site via the command line. Then you must know to authenticate your WordPress REST API. Finally, in case you want to disable WordPress REST API, you can also do it. The working with WordPress REST API can be seen in three parts are given below:

Accessing WordPress REST API using WP-CLI

WordPress Command Line Interface is abbreviated as WP-CLI. You can access WordPress from your PC’s command prompt. For this, you must have WP-CLI installed with all Kinsta hosting plans. You must open the command line terminal on your PC if it’s Mac or Linux. In case you have Windows, open Command Prompt.

WordPress REST API- Command prompt in windows

You will need the correct directory structure from the command line to access the local site. It is better to first try the WordPress REST API on your local site before trying it live. The following code snippet will show you the method to access the WordPress REST API for your site.

http://yoursite.com/wp-json/wp/v2

After this, you can use the suitable elements known as endpoints to access the data.

WordPress REST API Authentication

After accessing your site from the command line, you need to authenticate your process. This is to ensure the entire process is secured and you get access to all the private sites also. For authenticating your site through WP-CLI, you must install the authentication plugin first. You must install the Basic Auth plugin for development installations.

To access the live sites, you must install the JWT Authentication plugin. This plugin makes use of the JSON Web Token and makes it a more secure and robust plugin.

This authentication process will also take place using the command line. The following example is used to test the connection to WordPress. The output of this command will be a list of draft posts.

curl -X GET --user username:password -i http://yoursite.com/wp-json/wp/v2/posts?status=draft

Usually, the draft posts are private and hence request authentication to view them. While the public posts do not require any authentication. All the published posts are public, and you can use the following command to view them.

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

Disable WordPress REST API

In case, you do not want to access data from your site using the WordPress REST API, you have an option to disable it. But you can disable the access of private data only because the public data be accessed by everyone already.

For this, you must install the Disable WordPress REST API plugin. This will disable the REST API if you’ve not logged in to your website.

The following command lines will disable the REST API for your site.

add_filter( 'json_enabled', '__return_false' );
add_filter( 'json_jsonp_enabled', '__return_false' );

WordPress REST API Commands

The following WordPress REST API commands will be used to communicate with your site. The commands that are mostly used are:

GET

The GET command is used to retrieve the data from the site. This data can be your WordPress posts or some other data.

The following example will get all the articles published in your WordPress.

 GET http://yoursite.com/wp-json/wp/v2/posts/?status=published

The following example will fetch you the last post of your WordPress.

GET http://yoursite.com/wp-json/wp/v2/posts/?per_page=1

So, these are a few examples of the command GET. Basically, this GET command is used to pull content or data from your WordPress site.

POST

The POST command is used to add new content or data to your WordPress site.

The following example is used to create a new post. This will create an empty draft post and further can be updated with the necessary content.

POST http://yoursite.com/wp-json/wp/v2/posts/

You can also use the POST command to include other resources like adding attachments and other post types.

The following example is used to add an additional page to your WordPress site. This creates an empty page similar to the previous command.

POST http://yoursite.com/wp-json/wp/v2/posts/pages

PUT

You can edit an existing resource with the PUT command. This command is used to check the draft posts on your site and update one of them to publish. You can update all your posts using the PUT command.

The following command is used to get all the draft posts

POST http://yoursite.com/wp-json/wp/v2/posts/?status="draft"

From the list of all the current draft posts, you can change the status of one of the draft posts using its ID. The following command does this for you.

PUT http://yoursite.com/wp-json/wp/v2/posts/567

You can alter the status argument to publish or shelf the posts. These commands will allow you to post and edit the posts.

{
"status" = "publish"
}

To add content and then publish, the following command can be used.

{
"status" = "publish"
"content" = "content here"
}

When you get a response status with 200-OK, this indicated that the PUT request has edited your post successfully.

DELETE

The DELETE command is used to delete a resource. This command does not delete a resource permanently, instead, it puts the post in the trash.

The following command is used to move your post to trash.

DELETE http://yoursite.com/wp-json/wp/v2/posts/567

If you want to permanently delete a post instead of moving it to trash, then you must use the force argument. When you use this argument, the post gets deleted permanently and you cannot undo your action. So you need to be extra careful while using the DELETE command with force argument.

DELETE http://yoursite.com/wp-json/wp/v2/posts/567?force=true
Integrate WordPress via MySQL to BigQuery
Integrate WordPress via MySQL to PostgreSQL
Integrate WordPress via MySQL to Azure Synapse Analytics

Real-World Applications using the WordPress REST API

For the future of WordPress, the REST API opens up some interesting possibilities. Here are some examples of apps and websites that use the WordPress REST API to build SPAs or connect WordPress to other websites and technologies.

WordPress.com

To create a SPA that users interact with to administer their site, the WordPress.com admin screens are totally constructed utilizing the REST API.

This allows for dynamic communication between the UI and the server, resulting in a user-friendly interface that WordPress.org will most likely imitate in the future.

The Block Editor (Gutenberg)

The Gutenberg block editor uses the REST API to interface with your database and builds blocks as it does in the WordPress admin screens, but this time in self-hosted WordPress as well.

The REST API must be enabled for post types to work with the Gutenberg editor. That means if you’re using Gutenberg and registering a custom post type, you’ll need to add the following code to activate the block editor for that post type:

"show_in_rest" = true;

Event Espresso

Event Espresso is a WordPress plugin that allows users to create, manage, and promote events. It uses the REST API to allow users to access data from outside of WordPress. This means you may manage your events using mobile apps or SPAs.

Conclusion

From this blog, you will understand the complete workings of WordPress REST API. You will now have a brief knowledge of WordPress and WordPress REST API. The commands that are involved in working with WordPress REST API will be clear to you by now.

Streamline your web development by integrating WordPress with Python. Leverage Python’s capabilities within your WordPress site.

Hevo Data offers a faster way to move data from 150+ Data Sources, such as REST API for free, into your Data Warehouse. Hevo is fully automated and, hence, does not require you to code. Hevo provides a pre-built Native REST API Connector that will allow you to integrate data from many custom and non-native sources.

Want to take Hevo for a spin? Sign Up here for a 14-day free trial and experience the feature-rich Hevo suite firsthand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

Sherly Angel
Business Analyst, Hevo Data

Sherly is a data analyst with a passion for data analysis and architecture. Currently a Business Analyst at Hevo Data, she specializes in advanced analytics tools like Tableau and Python, harnessing them to drive data-driven decision-making. With a Master's in Healthcare Data Analytics and a PGP in Data Science, Sherly excels in designing scalable data solutions that optimize business processes and enhance operational efficiency.