getting started with wordpress json rest api

15

Click here to load reader

Upload: hishikawa-takuro

Post on 19-Jun-2015

875 views

Category:

Technology


2 download

DESCRIPTION

What is, and how to use WordPress JSON REST API. Talked at WordCamp Tokyo 2014

TRANSCRIPT

Page 1: Getting Started with WordPress JSON REST API

Getting Started with WordPress JSON REST APITakuro Hishikawa concrete5 Japan Inc. !

An author of Really Simple CSV Importer plugin (20,000+ download! Thanks!)

Page 2: Getting Started with WordPress JSON REST API

What Is the JSON REST API?

• Started as GSOC project

• Now provided as plugin

• Planned merge into core at future release

Page 3: Getting Started with WordPress JSON REST API

–http://wp-api.org/

“Access your WordPress site’s data through an easy-to-use HTTP REST API.”

Page 4: Getting Started with WordPress JSON REST API

–Ryan McCue http://wptavern.com/ryan-mccue-on-creating-the-json-rest-api-for-wordpress

“JSON is a data format based on Javascript’s representation of objects, but it’s widely used because it can be easily

represented in almost every programming language.”

Page 5: Getting Started with WordPress JSON REST API

Resources

• Posts (Create/Retrieve/Edit/Delete)

• Meta (Create/Retrieve/Edit/Delete)

• Media (Create/Get)

• Users (Create/Retrieve/Edit/Delete)

• Taxonomy/Terms (Retrieve)

Page 6: Getting Started with WordPress JSON REST API

Building App Example

• Framework = concrete5

• Library = Zend_Oauth, Zend_Http_Client

Page 7: Getting Started with WordPress JSON REST API

Demo

• Get posts from WordPress

• Get the specific post from WordPress

• Insert a new post to WordPress

Page 8: Getting Started with WordPress JSON REST API

Step by step

Page 9: Getting Started with WordPress JSON REST API

Set up the plugin

• https://github.com/WP-API/WP-API/

• https://github.com/WP-API/OAuth1/

Page 10: Getting Started with WordPress JSON REST API

Get Access Token from WordPress

• Blog post (Japanese)

• http://notnil-creative.com/blog/archives/3301

Page 11: Getting Started with WordPress JSON REST API

Send HTTP Request to WordPress

$client = Loader::helper('wp_api','rest_wordpress')->getClient(); $client->setUri($wp_rest_api_url.’/posts'); $client->setMethod(Zend_Http_Client::GET); $client->setParameterGet('filter[posts_per_page]',$num); $client->setParameterGet(‘filter[cat]',$cat); $client->setParameterGet('filter[orderby]','title'); $client->setParameterGet(‘filter[order]','ASC'); $response = $client->request();

Page 12: Getting Started with WordPress JSON REST API

Send HTTP Request to WordPress

$client = Loader::helper('wp_api','rest_wordpress')->getClient(); $client->setUri($wp_rest_api_url.'/posts'); $client->setMethod(Zend_Http_Client::POST); $data = array( 'title' => $this->post('post_title'), 'content_raw' => $this->post('post_body'), 'status' => 'publish' ); $client->setRawData(Loader::helper('json')->encode($data),'application/json'); $res = $client->request();

Page 13: Getting Started with WordPress JSON REST API

Easy, right?

Page 14: Getting Started with WordPress JSON REST API

• As you can see, working with JSON REST API requires little knowledge of OAuth and building HTTP Request…

• Use library!JSON is a popular object type, OAuth is also popular

• Some issues…https://github.com/WP-API/OAuth1/issues/37https://github.com/WP-API/OAuth1/issues/34

Page 15: Getting Started with WordPress JSON REST API

Thanks!

Follow me! !

Twitter: @HissyNC GitHub: @hissy