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!

Recent Features

Incredible Demos

  • By
    The Simple Intro to SVG Animation

    This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery. An SVG element is a special type of DOM element...

  • By
    Digg-Style Dynamic Share Widget Using MooTools

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

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.

  20. Gaston

    Hi, i have a problem with your plugin. When i call json api from one category, the result is empty/blank page, but with the rest of categories works fine. I move all this posts to another NEW category and continue fail. I dont know what to do. The chrome debugger doesnt show nothing. http://rutadelaselva.com.ar/api/get_category_posts/?id=2
    Thanks

  21. marc

    No longer available. Is there an alternative?

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