WordPress JSON API Plugin

Written by David Walsh on September 20, 2010 · 19 Comments

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!

Comments

  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 November 1, 2011

    @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. Looks good, except needs things like pulling taxonomies. Anyone know how to do that?

  11. Chantal June 12, 2012

    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. 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. David Walsh – you are my new hero.

  14. 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. 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. How do I present more than 10 posts for a category tag?

  17. toyat July 5, 2013

    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.

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
HTML5's "email" and "url" Input Types
Newer
Create a Dojo-Powered WordPress Website View