How to integrate

Use Wordpress APIs to fetch your posts, tags, categories, comments etc

Wordpress is a hugely popular web platform that lets user build their own blogs, websites etc using a very user-friendly UI interface and a super-reliable PHP back-end. Now, Wordpress has integrated a REST API framework inside its core which is available to outside apps and web-apps using REST API endpoints. Using these endpoints, you can read, write, edit and delete your

  • Posts

  • Comments

  • Tags

  • Categories

  • Users

  • Media

... and much more.

Wordpress REST API

The WordPress REST API provides API endpoints for WordPress data types that allow developers to interact with sites remotely by sending and receiving JSON (JavaScript Object Notation) objects. JSON is an open standard data format that is lightweight and human-readable, and looks like Objects do in JavaScript; hence the name. When you send content to or make a request to the API, the response will be returned in JSON. This enables developers to create, read and update WordPress content from client-side JavaScript or from external applications, even those written in languages beyond PHP.

Why use the WordPress REST API

Using WP REST API, now you can use your Wordpress deployment as a back-end, that serves data to other web-app or mobile apps, similar to our case.

E.g. You have a News website based on Wordpress. You now want to publish an app on Google and Apple app stores. Going with conventional methods, you will have to create a separate back-end, and transfer the complete existing database to be used in the mobile app. Not to mention that both that databases need to stay in sync as you keep posting new data on your News website.

Solution You start using WP REST APIs from your Wordpress installation, and read, write, edit data right from your app, written in any language.

Basics of WP REST API

Wordpress REST APIs use a base URL of the form

http://oursite.com/wp-json/

where oursite.com is replaced by your website name.

GET requests

For simple data fetching requests, you use GET requests. E.g. to fetch all posts from your platform, you use

http://oursite.com/wp-json/wp/v2/posts

This will fetch all posts data in JSON format. GET requests are similar to calling a URL in your browser. The data in response should look something like this

[
    {
        "id": 1,
        "date": "2018-04-24T19:37:02",
        "date_gmt": "2018-04-24T19:37:02",
        "guid": {
            "rendered": "http://wordpress.dev/?p=1"
        },
        "modified": "2018-04-24T21:46:13",
        "modified_gmt": "2018-04-24T21:46:13",
        "slug": "lorem-ipsum-dolor-sit-amet",
        "status": "publish",
        "type": "post",
        "link": "http://wordpress.dev/lorem-ipsum-dolor-sit-amet/",
        "title": {
            "rendered": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        },
        "content": {
            "rendered": "

POST requests

To create any data in the platform, you use POST endpoints. In addition to calling an API endpoints, you also need to pass the required (and optional) data in the API request. A POST API call should like following

fetch('http://oursite.com/wp-json/wp/v2/posts',{
    method: "POST",
    headers:{
        'Content-Type': 'application/json',
        'accept': 'application/json',
        'Authorization': 'Bearer '+user.token
    },
    body:JSON.stringify({
        title: 'Lorem ipsum dolor sit amet',
        content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        status: 'publish'
    })
}).then(function(response){
    return response.json();
}).then(function(post){
    console.log(post);
});

Response of a created post would be in JSON. Complete schema of creating a post using POST endpoint can be checked here.

EDIT and DELETE requests are similar to calling above requests, just a little different result.

Complete documentation of Wordpress REST API can be found in Wordpress Rest API documentation

Last updated