[wp-hackers] Building a JSON API style plugin

Adriaan Pelzer adriaan at wewillraakyou.com
Thu Nov 3 10:50:12 UTC 2011


I usually implement a simple json api as one or more custom post types.

There are many different approaches to this. You can implement an entire
API in a single post type (and its corresponding php template) using $_GET,
$_POST, etc parameters, or you can organise it a bit more.

In a nutshell:

   1. Create a custom post type per call
   2. Create a post per resource (keep in mind its easy to automate this)
   3. Implement the JSON output in the single-<postslug>.php template file
   4. You can use the archive-<postslug>.php file for overview or "listing"
   calls

Example:

   1. custom post type: tickets
   2. Archive - returns list of tickets
   3. Single - returns details of a single ticket

You get the idea ...

Adriaan Pelzer

putting you in touch with your crowds
http://www.wewillraakyou.com
<http://www.wewillraakyou.com>twitter: http://www.twitter.com/adriaan_pelzer
linkedIn: http://uk.linkedin.com/pub/adriaan-pelzer/4/874/860/
skype: adriaan_pelzer
<http://uk.linkedin.com/pub/adriaan-pelzer/4/874/860/>
+4478 7978 1743



On Thu, Nov 3, 2011 at 6:32 AM, Tom Barrett <tcbarrett at gmail.com> wrote:

> On 3 November 2011 05:47, Andrew Spratley <aspratley at gmail.com> wrote:
>
> > Does this help http://wordpress.org/extend/plugins/json-api/ ?
> >
>
> Mmm.. yes and no. :) None of the ticket data is stored in the local
> WordPress database, so it can't be too reliant on using wp_posts.
>
>
> > On Wed, Nov 2, 2011 at 7:38 PM, Otto <otto at ottodestruct.com> wrote:
> > > If it was something I was putting in a plugin or releasing, then
> > > template_redirect is where I'd generally put it, with some additional
> > > rewrite rules to make the URLs pretty. This is fairly easy to do,
> > > actually. Say you wanted to have the URLs look like /ticket/12345:
> > >
> > > add_action('init','ticket_add_rewrite');
> > > function ticket_add_rewrite() {
> > >       global $wp;
> > >       $wp->add_query_var('ticket');
> > >       add_rewrite_rule('ticket/([0-9]+)/?$',
> > > 'index.php?ticket=$matches[1]', 'top');
> > > }
> >
>
> I think this is the little gem I'm looking for. That looks really simple
> and can easily handle grabbing data from a non WordPress database.
>
> --
> http://www.tcbarrett.com | http://gplus.to/tcbarrett |
> http://twitter.com/tcbarrett
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list