Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

WordPress APIs


Published on

Rambling Talk given at

Supplemental information at

Published in: Technology
  • Be the first to comment

WordPress APIs

  1. 1. WordPress APIsFeeds, XML-RPC, APP, and “REST” WordCamp OC - 2012/06/02 - Mike Adams
  2. 2. Hi• Mike Adams = mdawaffe• Automattic/ ~6 years• WordPress developer: ~8 years• Team Social Lead
  3. 3. Hi• Mike Adams = mdawaffe• Automattic/ ~6 years• WordPress developer: ~8 years• Team Social Lead Links/Examples: pee one ess dash three ZEE
  4. 4. WordPress Data over HTTP• Feeds• XML-RPC• Atom Publishing Protocol• “REST”
  5. 5. Evaluating• Provides useful data/ways to manipulate• Is easy to work with in any language• Well documented• Grokkable (human readable)• Command line is king
  6. 6. Feeds• RSS v. Atom: essentially the same• Historically, RSS has gotten more love
  7. 7. Feeds: Features• Get • Posts, Pages • Comments• Can use normal WordPress query args author_name=mdawaffe, search=wordpress, category_name=goats, page=2, ...
  8. 8. Feeds: Tools• Command Line: curl• PHP: SimplePie• WordPress: fetch_feed() Simple wrapper for SimplePie• JS: It’s just XML? DOM, jQuery, ...
  9. 9. Feeds: Examples• Via JS, get most recent post titles:• Via CLI, get URL of most recent post: curl | perl -pe BEGIN{$/=undef} s/.*?<item>.*?<link>(.*?)<.* /$1/s Not really CLI if you resort to Perl :)
  10. 10. Feeds: Examples• Count posts by Marla that contain the string “movie”. curl ?author_name=marla &search=movie | grep <item> | wc -l Nope: Won’t work
  11. 11. Feeds: Caveats• Not always a perfect representation of for-display content• Never a good representation of for-edit content• Pagination• Plugins that add structured data to feeds may only touch RSS (not Atom)
  12. 12. Feeds: Evaluation• Data: Posts, Comments Only. Read Only• Easy: Meh - Libraries• Docs: Lots of specs• Grok: Mostly• CLI: Not so easy
  13. 13. Feeds: Conclusion• Quick way of reading basic data, but caveats• Longstanding, standard formats• Parsers available in every language• For WordPress, RSS > Atom
  14. 14. XML-RPC• Remote Procedure Call = Function based• Developed by Dave Winer in 1999•
  15. 15. XML-RPC: RequestPOST /xmlrpc.php HTTP/1.1HOST: example.comContent-Type: text/xmlContent-Length: 311<?xml version="1.0"?><methodCall><methodName>wp.getPosts</methodName><params> <param> <value> <array> <data> <value><int>0</int></value> <value><string>username</string></value> <value><string>password</string></value> </data> </array> </value> </param></params></methodCall>
  16. 16. XML-RPC: ResponseHTTP/1.1 200 OKContent-Type: text/xmlContent-Length: 27000<?xml version="1.0"?><methodResponse> <params> <param> <value> <array> <data> <value> <struct> <member><name>post_id</name><value><string>26</string></value></member> <member><name>post_title</name><value><string>SpaceMonkey!</string></value></member> ... </struct> </value> <value> ...
  17. 17. XML-RPC: WordPress• WordPress offers considerable functionality: Read and Write• Requires Authentication• Supported on all WordPress sites (Opt-in for sites)• Poorly Documented• Highly Extensible (via Plugin)
  18. 18. XML-RPC: Features• Get/Create/Edit/Delete • Posts, Pages • Comments • Terms, Taxonomies • Options• Media Uploads
  19. 19. XML-RPC: Auth• All (most) XML-RPC calls require authentication• WordPress.ORG • Username/Password • Extensible via Plugin
  20. 20. XML-RPC: Auth• All (most) XML-RPC calls require authentication• WordPress.COM • Username/Password • OAuth2
  21. 21. XML-RPC: Docs• XML-RPC is just a communication spec• Standard sets of Remote Procedures: Blogger, MoveableType, MetaWeblog, ...• WordPress supports them all!•• wp-includes/class-wp-xmlrpc-server.php
  22. 22. XML-RPC: Extensible• New Remote Procedures can be added with the xmlrpc_methods filter• Hooked function: • accepts array of arguments • returns most anything• XML parsing/serializing done by WordPress
  23. 23. XML-RPC: Tools• Command Line: Hard• PHP: xmlrpc_encode_request()• WordPress: WP_HTTP_IXR_Client• JS: Mimic
  24. 24. XML-RPC: Example$x = new WP_HTTP_IXR_Client($URL);$x->query( ‘wp.newPage’, array( 0, ‘user’, ‘password’, array( ‘title’ => ‘WordCamp’, ‘description’ => ‘OC, Baby!’ ), 1,));
  25. 25. XML-RPC: Caveats• Never a true representation of for-display content• Usually a true representation of for-edit content• Pagination impossible (until 3.4)
  26. 26. XML-RPC: Evaluation• Data: Lots of functionality• Easy: Meh - Libraries• Docs: No• Grok: No• CLI: No
  27. 27. XML-RPC: Conclusion• Pros • WordPress offers lots of functionality • Easy to extend• Cons • Caveats • Verbose XML • Poorly Documented
  28. 28. APP• Atom Publishing Protocol• RESTful - object based• Posts are represented as Atom Feed entries•
  29. 29. APP: WordPress• WordPress offers limited functionality• Requires Authentication• Supported on all WordPress sites (Opt-in for sites)• Poorly Documented• Harder to extend than XML-RPC
  30. 30. APP: Features• Get/Create/Edit/Delete • Posts, Pages• Media Uploads
  31. 31. APP: Auth• All APP calls require authentication • BASIC (Username/Password) • Extensible via Plugin
  32. 32. APP: Example
  33. 33. APP: Example ...
  34. 34. APP: Evaluation• Data: Posts only• Easy: Not really. At least it’s XML.• Docs: No• Grok: Looks like a feed• CLI: No
  35. 35. APP: Conclusion• Pro: Aesthetically cool• Cons • Not very well supported in WordPress • Hard to extend • Poorly documented• Don’t bother unless you are heavily invested in APP elsewhere already
  36. 36. “REST”ful JSON• WordPress.COM has launched a new RESTful JSON API• Soon in Jetpack• Will likely be in core (3.6?)
  37. 37. “REST”ful JSON• Fairly full featured• Public data does not require authentication• Private data requires OAuth2• True representation of both for-display, and for-edit content• Well documented
  38. 38. JSON: Features• Get/Create/Edit/Delete • Posts, Pages • Comments • Terms, Taxonomies• Media Uploads
  39. 39. JSON: “REST”$site • Read: GET /posts/$post_id • Create: POST /posts/new • Update: POST /posts/$post_id • Delete: POST /posts/$post_id/delete POST requests can be JSON or form encoded
  40. 40. JSON: Docs• Dynamically generated: always up to date•• Append “/help” to any API URL• API Console: AMAZING!
  41. 41. JSON: Tools• Command Line: curl• PHP: file_get_contents()• WordPress: wp_remote_request()• JS:Yes• Any language that can make remote HTTP requests and understand JSON(P)
  42. 42. JSON: Examples• Get most recent post• Get a post’s likes
  43. 43. JSON: Examples• Create a Post curl --data title=WCOC --data content=Rocks -H Authorization: BEARER *SECRET* https://public- sites/ posts/new
  44. 44. JSON: Evaluation• Data: Not as fully featured as XML-RPC• Easy:Yes• Docs:Yes!• Grok:Yes• CLI:Yes!
  45. 45. JSON: Conclusion• Pros • Easiest to use • Built in up-to-date documentation • True representation of for-display and for-edit content• Con: • Not as full featured as XML-RPC (yet!)
  46. 46. Bye• Mike Adams = mdawaffe
  47. 47. Bye• Mike Adams = mdawaffe• Thanks!