wordpress rest api - what, how and why?

32
WordPress REST API What, How & Why Nikhil Vishnu @nikhilvishnu

Upload: nikhil-vishnu

Post on 14-Jan-2017

352 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: WordPress REST API - What, How and Why?

WordPress REST APIWhat, How & Why

Nikhil Vishnu@nikhilvishnu

Page 2: WordPress REST API - What, How and Why?

Who am I?

I’m Nikhil Vishnu

● Co-founder and CEO of Mobapper● Working with WordPress for 7 years● Mobile App Developer● WordCamp speaker

Page 3: WordPress REST API - What, How and Why?

What is WordPress JSON REST API?

Page 4: WordPress REST API - What, How and Why?

JSONJavascript Object Notation

{"name" : "Nikhil Vishnu",

"company" : "Mobapper",

"hometown" : "Kottayam, Kerala",

"gender" : "male"

};

Page 5: WordPress REST API - What, How and Why?

REST Representational State Transfer

POST

GET

PUT

DELETE

Create

Read

Update

Delete

Page 6: WordPress REST API - What, How and Why?

APIApplication Programming Interface

"User Interface" that software uses to interact with other software.

Page 7: WordPress REST API - What, How and Why?

Twitter

Page 8: WordPress REST API - What, How and Why?

YouTube

Page 9: WordPress REST API - What, How and Why?

WordPress JSON REST API

“Easy to use REST APIs for retrieving or updating your WordPress website data in simple JSON format.”

Page 10: WordPress REST API - What, How and Why?

WordPress REST API

www.wp-api.org

Page 11: WordPress REST API - What, How and Why?

Why do we need it?

Page 12: WordPress REST API - What, How and Why?

WordPress External API

● XML RPC is very complicated

● 40% of WordPress iOS App code is just for cleaning up xml

Page 13: WordPress REST API - What, How and Why?

How to use WordPress REST API?

Page 14: WordPress REST API - What, How and Why?

Install Pluginhttps://wordpress.org/plugins/rest-api/

Page 15: WordPress REST API - What, How and Why?

What is Available?● wp-json

○ Show all the routes and end points● wp-json/posts

○ create, read, update and delete posts● wp-json/users

○ create,read,update, delete users● wp-json/media

○ create,read,update and delete media items● wp-json/taxonomies

○ read taxonomies and terms● wp-json/pages

○ create,read and delete pages● ...

Page 16: WordPress REST API - What, How and Why?

List all Endpoints

GET /wp-json

Page 17: WordPress REST API - What, How and Why?
Page 18: WordPress REST API - What, How and Why?

List Posts

GET /wp-json/posts

Page 19: WordPress REST API - What, How and Why?
Page 20: WordPress REST API - What, How and Why?

List Posts Parameters

● Filter[] : Accepts WP_Query arguments

● Page : Allows for pagination

● Context : Usage context ‘View’ or ‘Edit’

GET /posts?filter[posts_per_page]=8&filter[order]=ASC

GET /posts?page=1

Page 21: WordPress REST API - What, How and Why?

Retrieve A Post

POST /wp-json/posts/<id>POSTGET

Page 22: WordPress REST API - What, How and Why?

Edit A Post

POST /wp-json/posts/<id>POSTGETPUT

Page 23: WordPress REST API - What, How and Why?

Create A Post

POST /wp-json/posts/POSTGETPOST

Page 24: WordPress REST API - What, How and Why?

Taxonomies

POST /wp-json/taxonomies/POSTGETPOSTGET

Page 25: WordPress REST API - What, How and Why?

Taxonomy Terms

POST /wp-json/terms/categoryPOSTGETPOSTGET

Page 26: WordPress REST API - What, How and Why?

API Documentation

http://wp-api.org

Page 27: WordPress REST API - What, How and Why?

Authentication

● Cookie Authentication (client side)○ For themes & Plugins○ Using nonces for CSRF attacks

● OAuth 1.0○ For external apps○ WP Auth plugin

● HTTP Basic Auth○ For development○ WP Basic Auth Plugin

Page 28: WordPress REST API - What, How and Why?

Custom Endpoints<?php

function my_awesome_func( $data ) {

$posts = get_posts( array(

'author' => $data['id'],

) );

if ( empty( $posts ) ) {

return null;

}

return $posts[0]->post_title;

}

Page 29: WordPress REST API - What, How and Why?

Registering Route<?php

add_action( 'rest_api_init', function () {

register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(

'methods' => 'GET',

'callback' => 'my_awesome_func',

) );

} );

Page 30: WordPress REST API - What, How and Why?

Where we can use WP API?

Page 31: WordPress REST API - What, How and Why?

Where we can use it?

● Display and manage content in mobile app

● Custom dashboard

● Backbone.js themes/plugins

● Form validation

● Integrate content with other platforms

● Integrate other platforms with WordPress

● ...

Page 32: WordPress REST API - What, How and Why?

Questions?@nikhilvishnu

[email protected]