Explanation of the WordPress REST API

The WordPress REST API is a feature built into a custom plugin in newer versions of WordPress.

This feature helps the WordPress website interact with any external application, whether it is a web application, mobile application, or something else, by facilitating the process of sharing site data with these applications easily.

In today’s article, we will explain to you all the details about the REST API feature, its most important terms, and how to benefit from it on your site. We will explain in a practical way how to use it to obtain site articles and modify them easily, with an explanation of how to activate and deactivate this feature on your site if you wish not to use it.


What does API mean?

API repair is an abbreviation of the following words ( A application programming interface ) , which generally means creating a connection point between a specific application or software system; And between you as a backend developer.

Where the backend programmer can benefit from the services and results of this application or software system in the website or mobile application without having to rebuild it again.

For example, if you add Google Maps to your WordPress site , in this case you are using the Google Maps API to connect your WordPress site to Google Maps.

Likewise, when the Facebook Like button is added to a page on your WordPress site, this is done by linking the Facebook API to your site. You enable that web page to use the API to receive data (like button) and send data (like request).

What is the WordPress REST API feature

REST API, short for Representational State Transfer, is a type or standard of API that follows a programming approach that defines how web services communicate with each other over the Hypertext Transfer Protocol (HTTP).

There are many types of application programming interfaces (API), but REST is considered the most popular and newest. It works by processing text data for external applications without the need for direct access to the database of these applications.

The WordPress REST API is used in WordPress to help WordPress developers connect their WordPress site with other external websites, web applications, or mobile applications. It also helps developers access the site’s database, retrieve data from it, and add data to it.

Through this feature, you will be able to obtain the information you want from the WordPress website database in JavaScript Object Notation (JSON) format. It is a language specifically designed to send, receive and store data. As it stores data in pairs (key and value), you can then display this data in any other programming environment, even if this environment does not use the PHP language on which WordPress depends. 

For example, if you want to create a mobile application for your phone that does not depend on WebView, which usually displays an identical copy of all your website data within an iframe, and you want to pull only some site-specific data such as (articles, media, pages, classifications, etc. Of the specified data)
in this case you need to use a REST API that organizes this data and makes it available to the mobile application developer so that he can automatically reuse it in the application.

Importance of WordPress REST API

The REST API is included in modern WordPress versions (4.7 and later) implicitly in the form of an automatically activated REST API add -on to enable developers to share all WordPress site data with all external web or mobile applications, and facilitates the process of programming any application that relies primarily on its data from WordPress website, without it you will need to program the API for your website from scratch, which is very expensive.

In addition, the add-on helps you receive data from external parties also within your site and not just send it. On WooCommerce sites, for example, the add-on works to receive requests from any external application, saves them on your site in a simplified manner, and also re-sends the request data to the application in order to Shown to the customer.

This feature is also considered very important in all WordPress sites and is indispensable in any site, even if you do not need to create any external applications, as the REST API addition works in modern versions of WordPress in a built-in manner and is necessary for the functioning of some features such as the Gutenberg component editor .

This feature has enabled WordPress to evolve from just a website creation platform to become a full-fledged application framework. It enables developers to use it to create mobile and web applications with great ease.

Read also: Explaining Progressive Web Applications (PWAs), their features, and how to create them

In the following paragraphs, we will explain to you how to benefit from the WordPress REST API to fetch specific data from any WordPress site with practical examples.


Use a WordPress REST API plugin

First, before explaining the addition, you must know some basic concepts that any developer who will use this technology needs to understand its meaning:

Elementary concepts

  • Route : It is the link that enables you to access the site, with different certificates used, such as HTTP or Https.
  • EndPoint : It is the connection created between http and the Route. To reach an endpoint, you must use a path that takes the form of a regular URL. For example, the following path http://yourdomain.com/wp-json/ which It returns all content and metadata for your site in JSON format.
  • Request : It is a command issued by WP-Rest_Request, and it is used to fetch and store information for the current request.
  • Response  provides the data for which the Request was made, and if it is not available, an error message is forwarded with an explanation of the reason, so that you can fix it.
  • Schema: You can convert all inputs and properties into lists, which the Rest API can accept and redirect to use.
  • Controller Classes This is where you can manage all parts of the Rest API plugin.

Fetch data from website using API

In this part, we will explain how to fetch data from the site using the Rest API. First, you need to know that your entire interaction will be with your site’s domain via the wp json file path to the following page http://yourdomain.com/wp-json/

You should also know that there are three basic JSON requests that you will use with the REST API: (a GET request to retrieve data from the API, a POST request that enables you to send new information to WordPress, and a DELETE request to delete data)

In order to be able to fulfill these requests, follow these steps:

1- In order to access the WP-REST API, you will first need to access your site via the command line. This can be achieved on WordPress sites by installing the WP – CLI tool , which is a command line tool intended for developers. To enable them to manage tasks in WordPress. 

To install WP-CLI make sure that your hosting account supports SSH access as most shared hosting providers do not give you SSH access.

2- After installing the command line interface and connecting to your site through it, you must first test the connection and ensure that the data can be fetched from the required page, by typing the following command:

curl -X OPTIONS -i http://yourdomain.com/wp-json/

4. The following message will appear, which means the connection was successful:

HTTP/1.1 200 OK
Date: Wed, 23 Oct 2021 19:51:41 GMT
Server: Apache/2.4.29
X-Robots-Tag: noindex
Link: ; rel=”https://api.w.org/”
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Access-Control-Allow-Headers: Authorization, Content-Type
Allow: GET
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8

After a successful connection, you can download any content from the site. Write the following GET request first to get a JSON response that shows you the available paths you can fetch data from and the available endpoints within each path.

curl -X GET -i http://yourdomain.com/wp-json

The add-on is distinguished by its ability to fetch specific content from pages or articles only. If you would like to fetch content from articles published on your site only, use the following request:

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

You can see results similar to what you will get as a result of executing this request if you go to the following link on the WordPress website in Arabic https://wpar.net//wp-json/wp/v2/posts   where you will get a lot of information about all the site’s articles.

The information may not seem understandable to you at first, but it contains a lot of important information in JSON format such as ID, name, date, and texts of articles published on our site. You can then use this information in an application for example and display it in an understandable way using your own custom design.

If you want to fetch content from website pages only, use the following line:

curl -X GET -i http://yourdomain.com/wp-json/wp/v2/pages

You can also change the data that will be obtained by changing the parameters passed to the link in the previous code. For example, if you add per_page = 5 as follows

https://yourdomain.com/wp-json/wp/v2/posts?per_page=5 

You can get data for only 5 articles.

Thus, all you simply have to do is change the scrolling link and get the information you want from the site. The next stage is using this information by other platforms to display it appropriately (usually this information is analyzed using the JavaScript programming language and the data is output in an easy-to-read format using HTML).

Update location data using the API

The REST API is characterized by being very easy to use in order to modify the content of a specific article that you must initially specify. To specify specific content to modify or update, initially write the following line:

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

Then find the ID of that article you want to update, and add it in place of the ID in the next line:

curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts/<ID>

Now, for example, you can change the title of the article by using the following POST request and changing the value of the title with the new title by passing it as an argument as follows:

curl -X POST –user username:password http://yourdomain.com/wp-json/wp/v2/posts/PostID -d ‘{“title”:”My New Title”}’

There are many other methods you can use to make changes to an article. For example, you can change the category or rating of an article, change its publication status, or change its entire contents.

How to delete data from the site using the API

Now we will show examples of how to remove data from the site using the REST API. For example, to delete a user from the site. You can use the following DELETE request to delete a user with ID=10:

curl -X DELETE http://example.com/wp-json/wp/v2/users/10

This will remove the selected user from the site. You can also later use additional parameters to reassign this user’s articles to another user based on the user ID or permanently delete his articles from the site.

Note: Sometimes when executing some commands (such as commands to update or delete content) you may be asked to be able to register as an administrator. In order to be able to do this, you must install and activate the REST API Basic Auth plugin on your site.

After activating the add-on, open the CLI command line interface, then type the following command so that you can make a request on the REST API with administrator privileges, replacing the user name and password with the words for the admin account on the site. 

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

Then fetch any data you want from within the wp-json path as we explained. If you would like to view all the links and data that you can access on your site, we advise you to go to the following link: Reference handbook .

Thus, we have shown you some practical examples of how to obtain data through the REST API. All you have to do is learn the RESET API technology and learn the basics of JSON to acquire the basic skills for dealing with the data provided by your site.


If you are using an old version of WordPress and want to take advantage of this technology, you can simply activate the REST API plugin on your site as follows:

Enable WordPress REST API in older WordPress versions

As we mentioned at the beginning of the article, if you are using old versions of WordPress (version 4.7 or earlier), the REST API add-on will not be activated with the WordPress system by default, and in this case you will have to install the REST API add-on manually so that you can use it and benefit from its features. .

Note: You can find out the version of WordPress for your site by going to the bottom of the main admin page, where you will see the phrase “Thank you for using  WordPress,” and next to it is the phrase “version xxx,” where xxx represents the version number of WordPress used on your site.

To install this plugin on an old version of WordPress.

  1. Download the add-on from the following link: REST API add-on
  2. Upload the add-on to your site through: Add-ons>Add new>Upload add-on
  3. Click on the Activate Add-on button and the add-on will be activated successfully
  4. After that, you will not need to adjust anything, as the addition works on its own

Thus, you have ensured that the add-on is activated on your site.

Please note that if you install security plugins on your site, these plugins may disable the WP REST API feature activated on your site, so make sure to allow their use on your site.

Deactivate WordPress REST API

The REST API plugin is sometimes misused by unknown developers to create an app or website based primarily on data taken from your site, or in some other way.

So if you are not using the REST API features on your site directly and do not need them later, you may need to turn them off, especially if you are working with recent versions of WordPress that have the feature built-in. To do this, you must install and activate the Disable WP REST API plugin on your site.

You can also disable this feature manually through the file Function.php by adding code inside it. By entering the template editor, then choosing the template used, after that you will find the functions.php file at the beginning of the template files, then edit it and add the following code to it.

add_filter( ‘json_enabled’, ‘__return_false’ );
add_filter( ‘json_jsonp_enabled’, ‘__return_false’ );

Practical applications on the uses of adding the REST API

Below we give you some examples of practical applications created using the REST API plugin built into WordPress. 

1- The advanced Gutenberg editor

This application is one of the most famous applications on the RESET API feature, but how is it already present on your site?

The answer to this question is that the advanced Gutenberg editor implicitly uses the RESET API to access the database and create blocks.

2- UsTwo platform

UsTwo is a digital company whose website is built using the RESET API plugin as a single page application. The basic interface was programmed in the React language, while the backend is built on WordPress with some modifications to suit the site’s uses, and the idea of ​​​​the site’s operation depends entirely on the RESET API mechanism.

3- US TODAY website

Part of the famous news site USA Today relied on the RESET API mechanism to share the site’s content on other platforms such as Facebook Instant Articles, Apple News, and other news services that help spread news on the Internet.

Conclusion

In this article, we learned about the concept of the API, which is a function that connects different systems to each other. We explained how the RESET API works in WordPress and its most important features. We have provided some examples of the process through which you can manage the content of your site.

We advise you, as a WordPress developer, to delve deeper into the RESET API technology and learn the JSON format so that you can share WordPress website data with any party you want, and master the task of building external applications based on the content of WordPress websites easily.

Avatar photo
I am a young man who has been working in WordPress and e-marketing for 10 years. I would like to share my experience with you so that we can become professional in WordPress I will be happy to share the experience with you.