getting started with wp rest api
TRANSCRIPT
![Page 1: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/1.jpg)
#WCKTM2016
Facilitated By
Kishor Kumar Mahato
Abiral Neupane
Ashok Maharjan
A fast track Workshop
Getting Started with WPREST API
![Page 2: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/2.jpg)
#WCKTM2016
Who we are?
Kishor K. MahatoPython & WP Developer
Eagle Vision IT
Abiral NeupaneProject Manager & Web
Programmer
Eagle Vision IT
Ashok MaharjanWeb Programmer
Eagle Vision IT
2 . 1
![Page 3: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/3.jpg)
#WCKTM2016
Our Timeline
Presentation: 25 MinutesWorkshop: 1 hours 20 Minutes
2 . 2
![Page 4: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/4.jpg)
What will we cover today?
Why JSON REST API?Introduction to RESTQuick look on JSONIntroduction to WP RESTSetting up serverSetting up clientWorkflow of GET, POST, PUT and DELETE verbsBrief overview of authentication mechanism
#WCKTM2016
2 . 3
![Page 5: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/5.jpg)
What is REST?
1. REST stands for Representational State Transfer2. An Architectural style for networked hypermedia applications3. It is primarily used to build Web services that are lightweight,
maintainable, and scalable.4. A service based on REST is called a RESTful service
#WCKTM2016
3 . 1
![Page 6: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/6.jpg)
What is REST?
#WCKTM2016
3 . 2
![Page 7: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/7.jpg)
Features
RepresentationsMessagesURIsUniform interfaceStatelessLinks between resources
#WCKTM2016
![Page 8: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/8.jpg)
Representations
<Person> <ID>1</ID> <Name>M Vaqqas</Name> <Email>[email protected]</Email <Country>India</Country> </Person>
XML JSON{ "ID": "1", "Name": "M Vaqqas", "Email": "[email protected]", "Country": "India" }
#WCKTM2016
5 . 1
![Page 9: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/9.jpg)
What is JSON?
Abbreviation for "JavaScript Object Notation" Simply a way to describe data that is lightweight and extremelyeasy to use.
#WCKTM2016
5 . 2
![Page 10: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/10.jpg)
Why JSON over XML?
#WCKTM2016
5 . 3
![Page 11: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/11.jpg)
#WCKTM2016
< VERB >
Messages: Block Structure
< URI > < HTTP version>
< Request Header >
< Request Body >
< VERB >
6 . 1
![Page 12: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/12.jpg)
#WCKTM2016
Block Structure: VERB
GETPOSTPUTDELETE
6 . 2
![Page 13: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/13.jpg)
< VERB >
#WCKTM2016
Messages: Block Structure
< VERB > < HTTP version>
< Request Header >
< Request Body >
< URI >
6 . 3
![Page 14: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/14.jpg)
Block Structure: URI
Route for sending the requestExample:
https://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts
#WCKTM2016
6 . 4
![Page 15: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/15.jpg)
< VERB >
#WCKTM2016
Messages: Block Structure
< VERB > < URI >
< Request Header >
< Request Body >
< HTTP version>
6 . 5
![Page 16: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/16.jpg)
Block Structure: HTTP Version
Defines several resources based on versionIncludes:
AuthenticationSessionsRequest Methods
1.0 : GET, POST, HEAD1.1 : OPTIONS, PUT, DELETE, TRACE and CONNECT
Status Codes
#WCKTM2016
6 . 6
![Page 17: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/17.jpg)
< VERB >
#WCKTM2016
Messages: Block Structure
< VERB > < URI > < HTTP version>
< Request Header >
< Request Body >
6 . 7
![Page 18: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/18.jpg)
Block Structure: Request/Response
Request - a data load sent to the URL specifiedResponse - a data load sent as an acknowledgement of theRequestThe Request get received only if URL matches, and same goesfor Response
#WCKTM2016
6 . 8
![Page 19: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/19.jpg)
Block Structure: Request/Response
#WCKTM2016
{ "status": 200, "currentTimestamp": 1477447617, "message": "success", "data": { "name": "Test Demo ed ", "position": "Test post ed", "company": "Test Company ed", "id": "29" } }
https://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp-demo/attendee/29
6 . 9
![Page 20: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/20.jpg)
Messages: Request Structure (POST)
POST http://MyService/Person/ Host: MyService Content-Type: text/xml; charset=utf-8 Content-Length: 123 <?xml version="1.0" encoding="utf-8"?> <Person> <ID>1</ID> <Name>M Vaqqas</Name> <Email>[email protected]</Email> <Country>India</Country> </Person>
#WCKTM2016
7 . 1
![Page 21: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/21.jpg)
Messages: Request Structure (GET)
GET http://www.w3.org/Protocols/rfc2616/rfc2616.html HTTP/1.1 Host: www.w3.org Accept: text/html,application/xhtml+xml,application/xml; … User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 …Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8,hi;q=0.6
#WCKTM2016
7 . 2
![Page 22: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/22.jpg)
URIs
#WCKTM2016
GEThttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts
POSThttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts
DELETEhttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts/<id> PUThttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts/<id>
![Page 23: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/23.jpg)
Uniform Interface
#WCKTM2016
HTTPmethods
Resourcenames
Uniforminterfaces
![Page 24: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/24.jpg)
Stateless
#WCKTM2016
Text
![Page 25: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/25.jpg)
Links between resources
#WCKTM2016
http://example.com/api/v1/messages
[ { "id": 12345, "text": "Hello, world!" }, { "id": 12346, "text": "Testing, testing" }, ... ]
GET
![Page 26: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/26.jpg)
About WP API
#WCKTM2016
Gives us ability to access WordPress's site dataThe API is rich with functionality. Explore the documentationStarted as a plugin, but will be completely added into core from version4.7
12 . 1
![Page 27: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/27.jpg)
Why WP REST API?
#WCKTM2016
12 . 2
![Page 28: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/28.jpg)
Routes out of the box
#WCKTM2016
/wp-json/ Shows all the routes and endpoints available
/wp-json/posts/ Create, read, update, and delete posts
/wp-json/users/ Create, read, update, and delete users
/wp-json/media/ Create, read, update, and delete media items
/wp-json/taxonomies/ Read taxonomies and terms
/wp-json/pages/ Create, read, update, and delete pages
12 . 3
![Page 29: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/29.jpg)
Authentication
#WCKTM2016
12 . 4
![Page 30: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/30.jpg)
Authentication
#WCKTM2016
Cookie AuthenticationOnly applicable if the REST API is used within WordPress
OAuth AuthenticationNeed to use OAuth Server plugin
Basic AuthenticationCan be used ONLY during developmentUses Username & Password as security detailsHas plugins for assisting the process
12 . 5
![Page 31: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/31.jpg)
Prepare yourself
Install WordPressInstall WP REST API PluginInstall Post Man applicationPrettify the permalink
#WCKTM2016
13 . 1
![Page 33: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/33.jpg)
Thanks ( for Now ) !
Any Questions?
#WCKTM2016
![Page 34: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/34.jpg)
Let's Start
#WCKTM2016
15 . 1
![Page 35: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/35.jpg)
How will we learn?
#WCKTM2016
Lesson 1: Understanding Client and Route1. Learn to use Post Man2. CRUD on default route
Lesson 2: Registering your own route1. Register new route2. CRUD on custom route
15 . 2
![Page 37: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/37.jpg)
#WCKTM2016
Basic: Learn to use Post Man
15 . 4
![Page 38: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/38.jpg)
#WCKTM2016
Moving onto Defaults
Go to Exercises > Lesson 1 folderYou will find Routes.txt ; Open itBased on the URI, Verb, and Body ( if needed ) provided, sendrequest using Post man
15 . 5
![Page 39: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/39.jpg)
#WCKTM2016
Grab the Post
[ { "id": 11, "date": "2016-09-21T04:18:48", "date_gmt": "2016-09-21T04:18:48", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/09/21/title-from-rest-api-5/" }, "modified": "2016-10-25T16:55:42", "modified_gmt": "2016-10-25T16:55:42", "slug": "title-from-rest-api-5", "type": "post", "link": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/09/21/title-from-rest-api-5/" "title": {
/wp-json/wp/v2/postsGET
Response:
15 . 6
![Page 40: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/40.jpg)
#WCKTM2016
Add the Post
/wp-json/wp/v2/postsPOST
{ "id": 31, "date": "2016-10-26T06:01:32", "date_gmt": "2016-10-26T06:01:32", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" "raw": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" }, "modified": "2016-10-26T06:01:32", "modified_gmt": "2016-10-26T06:01:32", "password": "", "slug": "post-from-postman", "status": "publish", "type": "post",
Response:{ "title": "Post From postman ", "content": "Lorem text", "status": "publish" }
Request
15 . 7
![Page 41: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/41.jpg)
#WCKTM2016
Delete the Post
{ "id": 31, "date": "2016-10-26T06:01:32", "date_gmt": "2016-10-26T06:01:32", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" "raw": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" }, "modified": "2016-10-26T06:01:32", "modified_gmt": "2016-10-26T06:01:32", "password": "", "slug": "post-from-postman", "status": "publish", "type": "post",
/wp-json/wp/v2/posts/<id>DELETE
Response:
15 . 8
![Page 42: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/42.jpg)
#WCKTM2016
Update the Post
{ "id": 34, "date": "2016-11-16T14:53:38", "date_gmt": "2016-11-16T14:53:38", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/11/16/post-from-postman-444/" "raw": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/11/16/post-from-postman-444/" }, "modified": "2016-11-16T14:53:52", "modified_gmt": "2016-11-16T14:53:52", "password": "", "slug": "post-from-postman-444", "status": "publish", "type": "post",
/wp-json/wp/v2/posts/<id>PUT
Response:
15 . 9
![Page 43: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/43.jpg)
#WCKTM2016
Advanced: Register new Route
1. Go to Resources folder and you will find wcktm20162. Copy the plugin and Activate it.3. Go to Exercises > Lesson 24. Open get-attendees-route.php and copy the code5. Open the plugin - wcktm2016 > wcktm2016.php file6. After the comment /* Your code for route here */ paste your code7. Back to Exercises > Lesson 2, Open get-attendees-callback.php
and copy the code8. In wcktm2016.php file after comment /* Your code for route
callback here */ paste the code
15 . 10
![Page 44: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/44.jpg)
#WCKTM2016
Open Postman and enter following details in it:
Advanced: Using the Route
Click on Send button
Verb: GET URI: https://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wcktm2016/attendees
15 . 11
![Page 45: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/45.jpg)
#WCKTM2016
Repeat the same
Go to Exercises > Lesson 2Find other snippets and do the same like you did earlier
15 . 12
![Page 46: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/46.jpg)
Bingo!!!You did It
15 . 13
![Page 47: Getting Started With WP REST API](https://reader030.vdocuments.site/reader030/viewer/2022020108/5878760f1a28ab497b8b7425/html5/thumbnails/47.jpg)
Thank you !
#WCKTM2016
148910111416