Getting Started with WordPress JSON REST APITakuro Hishikawa concrete5 Japan Inc. !
An author of Really Simple CSV Importer plugin (20,000+ download! Thanks!)
What Is the JSON REST API?
• Started as GSOC project
• Now provided as plugin
• Planned merge into core at future release
–http://wp-api.org/
“Access your WordPress site’s data through an easy-to-use HTTP 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.”
Resources
• Posts (Create/Retrieve/Edit/Delete)
• Meta (Create/Retrieve/Edit/Delete)
• Media (Create/Get)
• Users (Create/Retrieve/Edit/Delete)
• Taxonomy/Terms (Retrieve)
Building App Example
• Framework = concrete5
• Library = Zend_Oauth, Zend_Http_Client
Demo
• Get posts from WordPress
• Get the specific post from WordPress
• Insert a new post to WordPress
Step by step
Set up the plugin
• https://github.com/WP-API/WP-API/
• https://github.com/WP-API/OAuth1/
Get Access Token from WordPress
• Blog post (Japanese)
• http://notnil-creative.com/blog/archives/3301
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();
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();
Easy, right?
• 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
Thanks!
Follow me! !
Twitter: @HissyNC GitHub: @hissy