Getting Started with WordPress JSON REST API

  • Published on

  • View

  • Download


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


  • 1. Getting Started withWordPress JSON REST APITakuro Hishikawaconcrete5 Japan Inc.!An author of Really Simple CSV Importer plugin(20,000+ download! Thanks!)
2. What Is the JSON RESTAPI?• Started as GSOC project• Now provided as plugin• Planned merge into core at future release 3. “Access your WordPress site’s data throughan easy-to-use HTTP REST API.”– 4. “JSON is a data format based onJavascript’s representation of objects, butit’s widely used because it can be easilyrepresented in almost every programminglanguage.”–Ryan McCue 5. Resources• Posts (Create/Retrieve/Edit/Delete)• Meta (Create/Retrieve/Edit/Delete)• Media (Create/Get)• Users (Create/Retrieve/Edit/Delete)• Taxonomy/Terms (Retrieve) 6. Building App Example• Framework = concrete5• Library = Zend_Oauth, Zend_Http_Client 7. Demo• Get posts from WordPress• Get the specific post from WordPress• Insert a new post to WordPress 8. Step by step 9. Set up the plugin•• 10. Get Access Token fromWordPress• Blog post (Japanese)• 11. Send HTTP Request toWordPress$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(); 12. Send HTTP Request toWordPress$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(); 13. Easy, right? 14. • As you can see, working with JSON REST API requireslittle knowledge of OAuth and building HTTPRequest…• Use library!JSON is a popular object type, OAuth is also popular• Some issues… 15. Thanks!Follow me!!Twitter: @HissyNCGitHub: @hissy