[wp-hackers] Building a JSON API style plugin

Tom Barrett tcbarrett at gmail.com
Wed Nov 2 15:51:14 UTC 2011


I want to add some trivial JSON requests to a site. I'm wondering if there
is a best practice for this. I have various thoughts on this, but nothing
terribly concrete yet.

1. Is template_redirect the best hook point?
2. If I don't want to use the query string (e.g. /api/ticket/1234.json)
will I get caught up in the rewrite system?

My initial version is this:
add_action( 'template_redirect', 'get_ticket' );
function get_ticket(){
  $ticket = get_ticket_details($_GET['ticket']);
  header('Content-Type: application/json');
  echo json_encode($ticket);
  exit(0);
}

Any glaring WordPress issues there? I appreciate there is no sanity or
taint checking, but it seems that WordPress has a lot of tricks under it's
hood and I'm trying not to rewrite the wheel.

-- 
http://www.tcbarrett.com | http://gplus.to/tcbarrett |
http://twitter.com/tcbarrett


More information about the wp-hackers mailing list