Ionic 5 Full Starter App
  • Enappd Apps
  • Ionic 5 Full App
    • What is Ionic 5 Full App?
    • Template Features
    • Initial Setup
      • Environment Configuration
      • Credentials
    • Running the app
    • Deploying app as PWA
    • Building App on device
    • How to use this template?
      • Copy a module to another app
      • Shared Component & Modules
      • Map Service
    • Beginner Pack Features
      • Firebase
      • Layouts
      • Sidemenus
      • Login and Signups
      • Chat screens
      • Chat Lists
      • Video Playlists
      • Grids and Lists Layouts
    • Startup Pack Features
      • Wordpress
        • Wordpress JSON API Basics
        • Wordpress in Ionic 5 Full App
      • Translation - Multi-language
      • Using Custom Fonts
      • Infinite scroll
      • Content Loaders
      • Pull to refresh
      • List re-ordering
      • Date Pickers
    • Pro Pack Features
      • Phaser Game Framework
      • AdMob Integration
        • AdMob Introduction
        • Setting up Google Admob
        • Integration
      • Social Sharing
      • QR and Barcode Scanning
      • Google Places
      • Google Autocomplete
      • Social Logins
        • Google Login
        • Facebook Login
        • Twitter Login
      • Woo-commerce Integration
    • Removing a Page / Component
    • Removing a plugin
    • FAQs
    • Changelog
    • Troubleshoot
Powered by GitBook
On this page
  • Wordpress REST API
  • Why use the WordPress REST API
  • Basics of WP REST API
  • GET requests
  • POST requests

Was this helpful?

  1. Ionic 5 Full App
  2. Startup Pack Features
  3. Wordpress

Wordpress JSON API Basics

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

PreviousWordpressNextWordpress in Ionic 5 Full App

Last updated 6 years ago

Was this helpful?

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 (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);
});

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

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

Complete documentation of Wordpress REST API can be found in

JSON
here
Wordpress Rest API documentation