WordPress REST API: what it is and how to use it –

Hosting Mautic QuickStart -50% with support in Spanish
24 hours and free training

Send up to 1,000,000 emails/year. Mautic support in Spanish 24 hours, 365 days

If you use WordPress, you have surely heard that it is a very flexible software since we can extend functionalities at the level of plugins, themes, etc.

However, there is another feature that makes the software very flexible and that is precisely that it comes integrated with a REST API functionality.

The use of this CMS is not limited to consulting a website in a browser, but through the WordPress REST API it is possible to consult the data of your site, for example: from a desktop application, a mobile application or another external web service.

In this article we are going to see what the WordPress REST API is and how you can start using it.

What is REST API?

In short, one REST API is a programming interface which uses the HTTP protocol as a base, forming URLs to manage data and returning values ​​in JSON format.

In the above definition there are some terms that are detailed below:

REST (Representational State Transfer)

Transfer of Representational State.

It defines the architecture used in a network, it uses the HTTP protocol as a basis and it is used to generate data and operations by returning data in JSON or XML format. Since it uses HTTP as a base, all objects are handled via a URL

API (Application Programming Interface)

Application programming interfaces.

It is a formal specification of how one software module communicates with another, i.e. is a set of commands, functions, and protocols that allow developers to create specific programs for certain systems without having to write everything from scratch.

See also  CSS course in WordPress -

With the APIs we can, for example, on our website that communicates with the APIs of a bank to validate the payment and return the status of the transaction to our site.

HTTP Protocol (Hypertext Transfer Protocol)

HyperText Transfer Protocol.

It’s a simple client-server protocol that articulates the exchanges of information between Web clients such as browsers and HTTP servers.

It is the communication protocol used on the internet. HTTPS is based on this protocol, that is, it is an HTTP with security.

URL (Uniform Resource Locator)

Uniform Resource Locator.

The address that you type in the web browser bar is a URL, it is a unique address for each resource available on a web server, and the links that you create as part of your content are also URLS.

JSON (JavaScript Object Notation) format

Javascript object notation.

It’s a plain text format for data exchange; It emerges as an alternative to XML since its use and manipulation are much simpler.

WordPress REST API

All of the above definitions are fine, but how does this apply to WordPress?

Would you like to manage your posts from a mobile application? Well, that is precisely what is achieved through the WordPress REST API.

The WordPress REST API functionality basically consists of we can make use of our content without the need to consult it only through a website.

As we see in the following image, it is possible to manage content using applications other than a browser.

Initially this functionality was being developed as a separate plugin, however at the end of 2015 it was included as part of the WordPress 4.4 core.

See also  resize wordpress featured image

The use of this functionality is not limited to WordPress core only, there are some plugins that also make use of a REST API, for example WooCommerce.

Querying the WordPress REST API

In current versions of WordPress we already have the REST API available by default so we can make queries directly in the browser using a url syntax.

We must add /wp-json/wp/v2/ to the request url, and additionally add some end-point.

The available end-points can be seen in the . For example, to obtain the list of entries we would use: /wp-json/wp/v2/poststhat is, our query url would be:

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

We must bear in mind that a maximum of 10 tickets will be returnedalthough it is also possible add additional parameters to specify pagination and number of entries returned.

For example, if we want to see the second page of results we would use:

https://yourdomain.com/wp-json/wp/v2/posts?page=2

The following image shows a query to the WordPress REST API to return posts; a browser has been used and the data returned is in JSON format.

Keep in mind that not all browsers display JSON correctly, you could also try using an application to query the WordPress REST APIs.

The following image shows the same REST API query using the Postman application:

Logically, it is also possible to make this query to the REST API directly through programming:

$url = “https://yourcompany.site/wp-json/wp/v2/posts”; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); curl_close($ch); var_dump(json_decode($result, true));

In the above code we have used PHP, however it is possible to use any other programming language.

See also  How can I resize blog images on my...

Other operations using the REST API

In the case of reading public data from a website, we can use the REST API directly.

Obviously, the use of the WordPress REST API is not only limited to read operations, we can also create, update and delete data. However, for this we need some authentication method.

Some authentication methods are: Base authentication, OAuth authentication, Cookie authentication.

There are plugins that can facilitate the authentication workfor example, the plugin

To send the authentication values ​​it will be necessary to do it by programming, you can also use an application like to perform these operations as we saw before when performing read operations.

Explanatory video

In the following video we will see a summary of what is explained in this article and we will discover what it is and how to use the WordPress REST API with a practical example.

conclusion

As we have seen, the REST API functionality turns your WordPress website into a powerful content publishing tool that can be used from other applications built in various programming languages ​​and not just limited to the use of a browser.

Help us improve our content by sharing your opinion

WordPress technical support at and regular speaker at Meetups and WordCamps in America.

Loading Facebook Comments ...
Loading Disqus Comments ...