Successfully reported this slideshow.
Your SlideShare is downloading. ×

WP REST API - Building a simple Web Application

Advertisement

More Related Content

Advertisement

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

WP REST API - Building a simple Web Application

  1. 1. WP REST API Building a Simple Web Application WordCamp Singapore 2019
  2. 2. https://wpug.sg/join-us-on- slack/
  3. 3. Edmund Chan edmund@nerb.com.sg Developer
  4. 4. WP REST API https://www.slideshare.net/EdmundChan31/an- introduction-to-the-wp-rest-api
  5. 5. 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
  6. 6. 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)
  7. 7. 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
  8. 8. Building the App What we will be building today…
  9. 9. Building the App
  10. 10. 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.
  11. 11. 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
  12. 12. 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
  13. 13. Building the App https://yoursite.com/ wp-json/wp/v2/posts
  14. 14. 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
  15. 15. Building the App Retrieve Posts Method: GET Endpoint: /wp/v2/posts
  16. 16. Building the App Create Post Method: POST Endpoint: /wp/v2/posts
  17. 17. Building the App Delete Post Method: DELETE Endpoint: /wp/v2/posts/(id)
  18. 18. Building the App Update Post Method: PUT Endpoint: /wp/v2/posts/(id)
  19. 19. https://wpug.sg/join-us-on- slack/
  20. 20. Edmund Chan edmund@nerb.com.sg Developer

Editor's Notes

  • 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.

×