Treehouse

WordPress JSON API Plugin

By on  

Ever since I moved to a mostly-JavaScript job, I've become more and more enamored with the simplicity and usefulness of JSON.  Now I want everything available in JSON format, and when I encounter a service which doesn't provide a JSON API, I have a fit.  I was recently brainstorming a way to place my website into a Dojo/Dijit widget and realized that WordPress doesn't provide a native JSON method of retrieving post information.  Enter the awesome JSON API plugin.

Download & Install the JSON API Plugin

The plugin is available at the WordPress website:

http://wordpress.org/extend/plugins/json-api/

Download and install from the WordPress Admin interface.

Configure the Plugin

The plugin features three main functionality:

  • Core:  Basic methods for pulling standard post/category/tag/etc. data.
  • Posts:  Provides additional functionality with regard to posts, including post creation.
  • Respond:  Provides comment and trackback submission methods.

Most people will only need the "Core" method enabled.  Advanced users can have a shot at post and comment management.

Using the Plugin

The plugin has a billion uses and methods, all of which are documented at the following address:

http://wordpress.org/extend/plugins/json-api/other_notes/

A few of the more useful methods include pulling all categories...

/*
url ?json=get_category_index
*/
{
  "status": "ok",
  "count": 3,
  "categories": [
    { ... },
    { ... },
    { ... }
  ]
}//....

...and retrieving all posts in a given category...

/*
url ?json=get_category_posts&slug=mootools
*/
{
  "status": "ok",
  "count": 10,
  "count_total": 79,
  "pages": 7,
  "category": { ... }
  "posts": [
    { ... },
    { ... },
    ...
  ]
}//....

...or just grabbing recent posts:

/*
url ?json=get_recent_posts&count=10
*/
{
  "status": "ok",
  "count": 10,
  "count_total": 79,
  "pages": 7,
  "posts": [
    { ... },
    { ... },
    ...
  ]
}//....

As I mentioned above, there are a dozens of parameters you can provide to the plugin to fetch JSON data.

Take the Good with the Bad

My only concern about this plugin is that it provides a anyone with the ability to sniff out all of your posts using a JSONP library.  That's not the burden of the plugin creator -- just a thought from me.

Look forward to a sample usage post from me soon!

ydkjs-6.png

Recent Features

  • Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API,...

  • CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome,...

Incredible Demos

  • MooTools History Plugin

    One of the reasons I love AJAX technology so much is because it allows us to avoid unnecessary page loads.  Why download the header, footer, and other static data multiple times if that specific data never changes?  It's a waste of time, processing, and bandwidth.  Unfortunately,...

  • Fix Anchor URLs Using MooTools 1.2

    The administrative control panel I build for my customers features FCKEditor, a powerful WYSIWYG editor that allows the customer to add links, bold text, create ordered lists, and so on. I provide training and documentation to the customers but many times they simply forget to...

Discussion

  1. I know exactly how ya feel on the JSON data. It requires no XML parsing and it’s super easy to use when communicating between JS and PHP.
    I’ve even found it very useful for storing data arrays in both text files and in database fields.
    If someone were wanting to make a flat-file database, I’d definitely say JSON format would certainly be the way, although I don’t see why anyone would bother with it considering how convenient and robust SQLite databases are.

    This is an awesome plug-in I recently looked at myself. Thanks for the share!

  2. I absolutely love this plugin and have used it a number of times. One slight concern to note, is that it provides a public write API for posts, so look into locking that down or removing it for super sensitive sites.

  3. This Plug in is truly amazing as it ca be used for a lot of ways and i am sure a lot people will truly start using this plug in after reading this post. I myself have tried this out, specialty is its storing Data style.

  4. I’d have to say, this plugin is quite bad-ass. I’ve used it a few times. Kudos to the dev!

  5. YQL is also a one-stop-shop for converting almost everything to JSON http://developer.yahoo.com/yql/.

  6. And… if I want to retrieve the custom fields related to the posts? How can I do this?

  7. Can’t wait to try this. Have got a golden idea for it. Thanks David!

  8. Charleshaa

    @denis, same here ! Anyone knows how we can do this ? Cheers !

  9. @denis @Charleshaa you can do can pull custom fields by adding custom_fields=field1,field2,etc to your query string

  10. John

    Looks good, except needs things like pulling taxonomies. Anyone know how to do that?

  11. Chantal

    Hi. Does anyone know if I have to do anything with the response string to be able to use it in Sencha Touch apps?

  12. Sam

    I’ve been looking around and am looking to see if this can be used to login users? I understand it can pull info from WordPress site but what if the info is protected (With paywall plugins like Wishlist)

    Is there a way to login to a users account at that blog?

  13. FED Hog

    David Walsh – you are my new hero.

  14. agent207

    Regarding Sam’s question on “if this can be used to login users”. Does anyone have an example of how this can be accomplished?

  15. sama

    I’m on the same place where Sam’s and agent207 are right now. Is there any way to post as a registered user? somelike apikey, etc .
    cheers!

  16. james

    How do I present more than 10 posts for a category tag?

  17. toyat

    Hello David,
    I have JSON like this :

    {“result”:[[{"idprogdi":"40","namafak":"Fakultas Hukum","namaprogdi":"FH-IH Ilmu Hukum"},{"idprogdi":"307","namafak":"Fakultas Hukum","namaprogdi":"JurMHUM Magister Ilmu Hukum"},{"idprogdi":"41","namafak":"Fakultas Ekonomi","namaprogdi":"FE-IESP Ekonomi Pembangunan"},{"idprogdi":"42","namafak":"Fakultas Ekonomi","namaprogdi":"FE-M Manajemen"},{"idprogdi":"43","namafak":"Fakultas Ekonomi","namaprogdi":"FE-AK Akuntansi"},{"idprogdi":"210","namafak":"Fakultas Ekonomi","namaprogdi":"MM Magister Manajemen"},{"idprogdi":"309","namafak":"Fakultas Ekonomi","namaprogdi":"FE-IESP Magister Ilmu Ekonomi "},{"idprogdi":"45","namafak":"Fakultas Pertanian","namaprogdi":"FP-BT Agronomi"},{"idprogdi":"44","namafak":"Fakultas Pertanian","namaprogdi":"FP-BP Budidaya Tanaman Perkebunan"}]]}

    How can I do with this in wordpress? Thanks

  18. Hey david,

    could you please guide more on Posts controller.

    Looking forward.

  19. Samuel Osoba

    Hello David,

    Please could you recommend a plugin for display data feeds from external JSON/XML API sources on my wordpress pages. I’ve toiled for a while and I’ve been able to find one.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!