Presentation slides for WordCamp Singapore 2019 Contributor's day. Visit https://www.edmundcwm.com/presentations/ to view other presentation resources.
WP REST API
• Representational State Transfer (RESTful) API
– Uses HTTP protocol (almost always) and its delivery methods
– Uses JavaScript Object Notation (JSON) format to exchange data
WP REST API
• Four main request methods (CRUD)
– GET (retrieve data to server)
– POST (send data to server)
– PUT (change or update data on server)
– DELETE (remove data from server)
WP REST API
• Why is it useful?
– Universal Connector from WP to external apps (JS, Ruby, Python,
iOS/Android etc
– Allows us to Create, Read, Update and Delete data from
anywhere
– We are not restricted to the admin dashboard screen
Building the App
• Purpose
– provide a quick and simple way to publish short messages.
– suitable for note-taking/micro-blogging.
– demonstrate the CRUD operation.
• Approach
– Use WordPress as the backend for storing and retrieving data
– Frontend will be built using jQuery
Note: this tutorial assumes you have basic knowledge of JavaScript
and jQuery. We will be utilising things like arrays, functions and AJAX.
Building the App
Server
MAMP running on PHP 7.1 and MySQL 5.7.23
https://www.mamp.info/en/mamp/
https://codex.wordpress.org/Installing_WordPress_Locally_on_Your_Mac_With_MAMP
WordPress
5.2.2 (has to be at least 4.7)
Plugins
Basic Auth (https://github.com/WP-API/Basic-Auth)
Browser
Chrome v76
Building the App
Getting started:
1. Setup WordPress
2. Install Basic Auth plugin - https://github.com/WP-API/Basic-Auth
3. Download app files - https://github.com/edmundcwm/micro-blog-wp-rest-
api
4. Visit - https://yoursite.com/wp-json/wp/v2/posts - to ensure everything is
fine
Building the App
TO-DOs:
1. Insert AJAX call to RETRIEVE all posts via the WP REST API
2. Insert AJAX call to CREATE post via the WP REST API
3. Insert AJAX call to DELETE post via the WP REST API
4. Insert AJAX call to UPDATE post via the WP REST API
A REST or RESTful API is a type of API that uses HTTP requests to perform CRUD operations
HTTP is the foundation of how we share data online. Essentially it comprises of two aspects – Request and Response e.g., type a url in the browser toolbar (request) and the server provides a response
Uses JSON format which is one of the most popular ways for transmitting data in web applications
With the REST API, it securely exposes your data to HTTP requests from external sources. A website or application on another server can use the REST API to retrieve information from your WP site
A REST or RESTful API is a type of API that uses HTTP requests to perform CRUD operations
HTTP is the foundation of how we share data online. Essentially it comprises of two aspects – Request and Response e.g., type a url in the browser toolbar (request) and the server provides a response
Uses JSON format which is one of the most popular ways for transmitting data in web applications
With the REST API, it securely exposes your data to HTTP requests from external sources. A website or application on another server can use the REST API to retrieve information from your WP site
Also known as HTTP verbs
In the context of WP
POST = use your app and create a new blog post
GET = display 10 latest posts in your app or retrieve users information
PUT = update blog post content
DELETE = remove blog post
This is also commonly known as the CRUD operations
Also commonly known as the CRUD operations, the REST API allows us to create, read, update and delete data form anywhere. This opens the door for all kinds of possibilities. We can create a mobile app that uses WP as the backend and then use the REST API to create, read, update and delete content. We don’t have to login to the admin dashboard to do all these anymore.
Use WP as a storage and retrieval system
-By using the WP REST API, developers can utilise WP’s infrastructure for storing and retrieving data and focus on developing apps powered by other languages like Python, Ruby and Java.