O'Reilly

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!

Track.js Error Reporting

Upcoming Events

Recent Features

  • 9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • 6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving,...

Incredible Demos

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

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

Recently on David Walsh Blog

  • OâReilly Velocity Conference â Amsterdam

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • CanIUse Command Line

    Every front-end developer should be well acquainted with CanIUse, the website that lets you view browser support for browser features.  When people criticize my blog posts for not detailing browser support for features within the post, I tell them to check CanIUse:  always up to date, unlike...

  • Generating Alternative Stylesheets for Browsers Without @media

    If your CSS code is built with a mobile-first approach, it probably contains all the rules that make up the "desktop" view inside @media statements. That's great, but browsers that don't support media queries (IE 8 and below) will simply ignore them, ending up getting the...

  • Serve a Directory with PHP

    Many developers have a giggle at PHP, even looking down at the language, but let's be honest:  most of our blogs are powered by it (WordPress) and it's a great language to dabble around with.  I cut my teeth on PHP, though I prefer to avoid PHP these days. But...

  • Tips you can Use to Build an Excellent eCommerce Experience

    There are many reasons to build a good eCommerce experience if you are planning to build an eCommerce website. Visitors can place items in their cart and leave your site without making a purchase. In fact, three out of every four of them will do so....