# Wordpress JSON API Basics

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&#x20;

* Posts
* Comments
* Tags
* Categories
* Users
* Media

... and much more.&#x20;

## 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](https://en.wikipedia.org/wiki/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.&#x20;

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.&#x20;

## 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.&#x20;

### GET requests&#x20;

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](https://developer.wordpress.org/rest-api/reference/posts/#create-a-post).

**EDIT** and **DELETE** requests are similar to calling above requests, just a little different result.&#x20;

Complete documentation of Wordpress REST API can be found in [Wordpress Rest API documentation](https://developer.wordpress.org/rest-api/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://enappd-apps.gitbook.io/apps/ionic-4-full-app/startup-features/wordpress/wordpress-json-api-basics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
