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.

Table of contents

What is WordPress?

WordPress REST API - wordpress logo
Image Source: s.w.org

WordPress is an Open-Source platform that makes it simple to build a website, publish content, and administer it. WordPress is used by over 40% of the top ten million websites on the internet. It’s a simple and user-friendly solution that makes it easier for Bloggers and Publishers to get their work online. The nicest part about WordPress is that it doesn’t require any coding experience to set up and maintain a website.

WordPress.com is a blogging platform first and foremost. While it may be used for simple WordPress websites without a blog, it was built to be a blog Content Management System (CMS), therefore it has a lot of functionality tailored to that use case.

WordPress houses drag-and-drop capabilities for creating a website from the ground up, as well as a large number of commercial and free themes that span practically every category. It enables you to create everything from Blogging Sites to Corporate Portfolios to E-Commerce Sites. WordPress is SEO friendly, allowing users to create material that appeals to both visitors and search engines.

What are WordPress REST APIs?

WordPress REST API- WordPress REST API logo
Image Source: wptavern

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 ad exchange 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 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 ETL with Hevo’s No-code Data Pipeline

A fully managed No-code Data Pipeline platform like Hevo Data helps you integrate and load data from  100+ Data Sources (Including 40+ Free Data Sources) to a destination of your choice in real-time in an effortless manner. Hevo further provides a Native REST API Connector for free to help you load data from custom and non-native data sources to your desired destination without writing a single line of code.

Get Started with Hevo for Free

Hevo with its minimal learning curve can be set up in just a few minutes allowing the users to load data without having to compromise performance. Its strong integration with umpteenth sources allows users to bring in data of different kinds in a smooth fashion without having to code a single line. 

Check out some of the cool features of Hevo:

  • Completely Automated: The Hevo platform can be set up in just a few minutes and requires minimal maintenance.
  • Connectors: Hevo supports 100+ Integrations to SaaS platforms, files, databases, and BI tools. It supports various destinations including Amazon Redshift, Google BigQuery, Snowflake, and Firebolt Data Warehouses; Amazon S3 Data Lakes; and MySQL, SQL Server, TokuDB, DynamoDB, PostgreSQL databases to name a few.  
  • Real-Time Data Transfer: Hevo provides real-time data migration, so you can have analysis-ready data always.
  • 100% Complete & Accurate Data Transfer: Hevo’s robust infrastructure ensures reliable data transfer with zero data loss.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources that can help you scale your data infrastructure as required.
  • 24/7 Live Support: The Hevo team is available round the clock to extend exceptional support to you through chat, email, and support calls.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  • Live Monitoring: Hevo allows you to monitor the data flow so you can check where your data is at a particular point in time.
Sign up here for a 14-Day Free Trial!

When Not to Use the WordPress REST API

The WordPress REST API is not always the best option to build a website or app. Before you use it for development, there are a few things to keep in mind:

Compatibility

If you use the REST API and your app is going to be used on devices that don’t support JavaScript or by users who are likely to turn it off, it won’t work.

This problem will not occur if your WordPress site is written in PHP and outputs HTML. Although devices that don’t use JavaScript are becoming less common, if you’re creating for them, the REST API won’t operate.

Working with the REST API will also cause issues if your users are likely to disable JavaScript. For accessibility or security reasons, some users disable JavaScript in their browsers.

Accessibility

JavaScript-based websites and apps aren’t necessarily as accessible as HTML-based ones.

This is mostly due to the way JavaScript is utilized to offer dynamic information, which may cause issues for persons with visual impairments or photosensitive epilepsy.

You can solve these issues by using the REST API to access your WordPress site and produce data in an accessible format, but it’s worth testing your site for accessibility while you’re constructing it.

SEO

Single-page applications (SPAs) that refresh frequently can cause SEO problems. This is because search engines may not index content that isn’t given when the page is first output.

Many websites are now SPA-powered, and Google and other search engines are indexing them appropriately. However, every site built with the REST API should undergo a full SEO assessment.

Getting Started with the WordPress REST API

To execute all of the calls in this WordPress REST API tutorial, you’ll use the command-line interface (CLI). You can use the CLI to connect with the REST API without having to develop any additional scripts to request and process data.

The first step is to launch a command-line interface (CLI) tool on your computers, such as Terminal for macOS and Linux or PuTTY for Windows. Copy your shared or dedicated IP address and use your SSH credentials to log in.

It is also recommended to use a sample site or local testing. Also, make sure it’s compatible with WordPress version 4.4 or higher.

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

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

Image Source

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)

Image Source

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

Image Source

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 would have understood the complete working 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.

Visit our Website to Explore Hevo

Hevo Data offers a faster way to move data from 100+ Data Sources such as REST API for free, into your Data Warehouse to be visualized in a BI tool. 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 a plethora of custom and non-native sources. All this without writing a single line of code and free of cost.

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

mm
Business Analyst, Hevo Data

Sherley is a data analyst with a keen interest towards data analysis and architecture, having a flair for writing technical content. He has experience writing articles on various topics related to data integration and infrastructure.

No-code Data Pipeline for REST API

Get Started with Hevo