[wp-hackers] Building a JSON API style plugin

Tom Barrett tcbarrett at gmail.com
Mon Nov 28 12:18:50 UTC 2011


I've come back onto this project.

On 2 November 2011 17:38, 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');
> }
>
> Then you visit the permalinks page to make it rebuild the rules, and
> voila. You'll get the ticket number with get_query_var('ticket'). So
> you'd change to something like this:
>
> add_action( 'template_redirect', 'get_ticket' );
> function get_ticket(){
>  $ticket_num = get_query_var('ticket');
>  if ( empty($ticket_num) ) return;
>  $ticket = get_ticket_details($ticket_num);
>  header('Content-Type: application/json');
>  echo json_encode($ticket);
>  exit(0);
> }
>

I tried this, but $ticket_num is never populated. I add those lines to a
theme functions.php, visit the permalinks page and save. There are no
ticket/ entries in the rewrite_rules option table entry.

Looking at http://codex.wordpress.org/Rewrite_API/add_rewrite_rule I can't
see what is wrong with it.
I don't think I want this:
http://codex.wordpress.org/Rewrite_API/add_rewrite_tag.

So I'm not quite sure where to look now?


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


More information about the wp-hackers mailing list