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-1.png

Recent Features

  • Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else?...

  • Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and...

  • How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to,...

Incredible Demos

  • background-size Matters

    It's something that makes all men live in fear, and are often uncertain of. It's never spoken, but the curiosity is always there. Nine out of ten women agree in the affirmative. Advertisers do their best...

  • Introducing MooTools NextPrev

    One thing I love doing is duplicating OS functionalities. One of the things your OS allows you to do easily is move from one item to another. Most of the time you're simply trying to get to...

  • CSS Vertical Centering

    Front-end developing is beautiful, and it's getting prettier by the day. Nowadays we got so many concepts, methodologies, good practices and whatnot to make our work stand out from the rest. Javascript (along with its countless third party libraries) and...

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.

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

Use Code Editor