The content man­age­ment system (CMS) WordPress enjoys great pop­ular­ity. Compared to other CMSs, it’s re­l­at­ively easy to use to create websites for different needs. Main­tain­ing websites is similarly easy for editors with good Office skills. What’s more, web ad­min­is­trat­ors can access more than 55,000 plug-ins that perform a range of website tasks and are largely available free of charge as a basic version.

Tip

We’ve sum­mar­ised all the ad­vant­ages for your WordPress website in the pricing models “Essential”, “Business”, and “Unlimited”. Benefit from a simple set-up process, in­teg­rated security, and our optimised platform for your Hosting for WordPress.

What is the WordPress REST API?

The WordPress REST API refers to an interface that allows com­mu­nic­a­tion with the database used by WordPress. API stands for “ap­plic­a­tion pro­gram­ming interface”. It enables com­mu­nic­a­tion between different programs according to a defined system. It’s com­par­able to a dialogue between two people who only un­der­stand each other when they use the same language. You can find more in­form­a­tion in our article on APIs.

The ab­bre­vi­ation REST means “rep­res­ent­a­tion­al state transfer”. The prin­ciples for in­form­a­tion exchange were developed by US computer scientist Roy Fielding in 1994:

  • Uni­form­ity: The URLs for accessing resources must be uniform, con­sist­ent, and ac­cess­ible via a common approach like GET.
  • Client-server sep­ar­a­tion: If the server-side tech­no­logy changes (e.g. WordPress), the client-side ap­plic­a­tion (e.g. app) must still be able to access it.
  • Stateless: The server doesn’t change its state due to a new request via the API and doesn’t store the request.
  • Cache cap­ab­il­ity: This provides for high speed and con­form­ity on the server or client side.
  • Layered system: Access is possible over multiple layers.
  • Code on demand (optional): Code for local execution is only sent to the client when needed.

In WordPress, these re­quire­ments have been standard in the core – i.e. the heart of the content man­age­ment system – since version 4.7. Before that, there was the plug-in “WP REST API (WP API)” until May 13, 2018. As of WordPress version 5.x, elements were added to the system core to expand the existing com­mu­nic­a­tion pos­sib­il­it­ies with other (web) ap­plic­a­tions and apps.

Managed Hosting for WordPress
Create your site with AI, we manage the rest
  • Stress-free, no matter your skill level with easy AI tools
  • Full cus­tom­isa­tion with themes and plugins
  • Hassle-free updates and less admin

What is the WordPress REST API used for?

The interface enables direct com­mu­nic­a­tion with the database used by WordPress, which contains all the in­form­a­tion of a website. This database is used with the database man­age­ment system MySQL – an open-source software program. In order to com­mu­nic­ate, there are commands that allow access via the “built-in” WordPress API. Several ap­proaches are possible here:

Terminal access to the database

The WordPress REST API allows access to the content of a WordPress website via the command prompt in Windows:

  • Right-click on the Windows symbol > “Run” > enter “cmd” in the window > “OK”
  • Activate the search in the task bar > enter “cmd” > click on the app “Command Prompt”

Or the Terminal in macOS:

  • Enter the key com­bin­a­tion “cmd” + [space] + “Terminal” and double-click on the search result
  • In the upper menu bar on the desktop, select “Go to” > “Service Programs”, and double-click on “Terminal” in the window that appears
  • In “Dock”, click on “Programs” > “Service Programs” > “Terminal”

Enter the actual commands in the flashing prompt on the screen, for example:

C:\Users\YourUserName>_

Enter the following in­struc­tion:

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

Press “Enter” to start and a few moments later the output will be visible on the screen. This is the basic data of the website with the permitted options (“OPTIONS”). Here you can see that the GET command is used, for example: “Allow: GET”.

Using the following command, you’ll get all the data from the database – identical in content to the approach below.

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

Using the WordPress REST API with browser access

The WordPress database content can also be accessed via a HTTP browser request. Here, JSON is used:

https://yourdomain.com/wp-json/
Fact

JSON stands for “JavaS­cript Object Notation”, which is a text-based format for ex­chan­ging data. It’s easy to learn and read and is versatile. Find out more in our tutorial on JSON LD notation and our article on cross-domain data querying with JSONP.

The accessed in­form­a­tion is presented like an open book. To peer deeper into the website content, the WordPress REST API with its extensive range of commands enables detailed requests for defined com­pon­ents from the database – such as re­quest­ing posts as follows:

https://yourdomain.com/wp-json/wp/v2/posts

This gives you an overview of all posts in the database including any existing versions, links, media content, authors, and much more. An in­di­vidu­al post is clearly indicated in the WordPress database with its ID:

If you then wish to display an in­di­vidu­al post in the browser, insert its ID as follows:

https://yourdomain.com/wp-json/wp/v2/posts/6576

This procedure can be carried out with all types of content, from posts, pages, media, authors, and more. The REST API Handbook con­tain­ing the complete command reference can be found on the Wordpress.org developer pages.

WordPress REST API: a practical example

Why do we need a WordPress API? Allow us to demon­strate with a simple, practical example. A new website aims to collect news from other internet providers. This will only work with the WordPress REST API if the providing websites are also pro­grammed in WordPress. Here, the contents are queried with their IDs on the selected servers.

In order to access the most up-to-date content at all times, there are various pro­gram­ming options that can be used – such as the script languages PHP or the JavaS­cript library jQuery. We can show this with two simple code examples for the WordPress API.

Reading out a post with PHP

To read out a post from another WordPress site, a code section is in­teg­rated on a HTML page between <body> and </body>. This page should be able to process PHP. In this case, with the file name “test-wp-rest-api.php”. The names of the fields to be displayed can be taken from the JSON query shown above (guide, title, link, content, etc.). The code section comprises a PHP script that retrieves in­form­a­tion and a HTML element that returns the read data via PHP:

<!--?php</codesnippet-->
$url = ‘https://www.mywebsite.com/wp-json/wp/v2/posts/6576'; // The JSON query path with ID
$data = file_get_contents($url); // Transfer the content into a variable
$mydata = json_decode($data, true); // Decode JSON
?>
<h1>PHP: Read out a post via WordPress REST-API</h1> // Heading
<div id="mydata"></div>
<!--?php echo $mydata['content']['rendered']; ?--> // Output of 'content' with the ID 6576

Reading out a post with jQuery

An al­tern­at­ive for reading out content is jQuery. In this case, the library needs to be in­teg­rated into the page header. Then, everything will also work with the file ending .html.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> // Integrate jQuery
<script>// The section for reading out the post data</codesnippet></script>
$(function() {
$.getJSON('https://mtmedia.de/wp-json/wp/v2/posts/6576', function(alldata) {
var contentElm = alldata.content.rendered
$(contentElm).appendTo("#mydata");
});
});
<h1>jQuery: Read out a post via WordPress REST-API</h1> // Heading
<div id="mydata"> Output of 'content' of the post with the ID 6576</div>

The result is identical to the browser view produced using the PHP output. Using jQuery, the contents are returned in the DIV container “mydata” without an ad­di­tion­al variable.

What about websites with other content man­age­ment systems?

The API is called WordPress REST API because it’s an integral component of WordPress pro­gram­ming. For this reason, the interface is only available if the queried webpages are also set up with WordPress.

At­tempt­ing a query will produce a 404 error message. For these websites, there are other ways to contact the CMS database.

Blocking access to the WordPress REST API

Not everyone is okay with others being able to simply read out in­form­a­tion from their website and even re-use it. To prevent this, there’s a barrier that can be activated in the WordPress backend.

The plug-in “Disable WP REST API” from the WordPress re­pos­it­ory needs to be activated after in­stall­a­tion – like any other plug-in – in order to stop un­au­thor­ised access. Other than that, no more steps need to be taken.

The status code “401” (un­au­thor­ised) means that the server has rejected the HTTP request either due to invalid or missing au­then­tic­a­tion. In contrast to “403”, however, au­then­tic­a­tion is possible.

Many websites are protected in this way to prevent automated content theft. Asking the website operator for per­mis­sion to use external content is not only good manners, it’s also necessary to respect copyright. To test your own website, you can simply set the plug-in to “disabled”. The database content will then be ac­cess­ible again. Data in the plug-in cannot be lost.

Go to Main Menu