[wp-hackers] Building a JSON API style plugin

Simon Wheatley simon at sweetinteraction.com
Tue Nov 29 12:31:26 UTC 2011


On Mon, Nov 28, 2011 at 2:34 PM, John Blackbourn
<johnbillion+wp at gmail.com> wrote:
> On 28 November 2011 14:31, Tom Barrett <tcbarrett at gmail.com> wrote:
>> Further experimentation shows that this works (functions.php):
>>
>> <?php
>> $wp->add_query_var( 'ticket' );
>> add_rewrite_rule( 'ticket/([0-9]+)/?$', 'index.php?ticket=$matches[1]',
>> 'top' );
>>
>> add_action( 'template_redirect', 'get_ticket' );
>> function get_ticket(){
>>  $ticket = get_query_var( 'ticket' );
>>  error_log( "ticket={$ticket}" );
>>  return;
>> }
>> ?>
>>
>> Does anyone know which action hook this would be best placed in?
>
> The init action should work fine for rewrite rules. You may want to
> try the code in a separate plugin instead of functions.php, although
> there should be no difference.

In addition I'd suggest hooking init early, in case someone else comes
along and flushes rewrite rules before your rewrite rule has been
added (which is conceivable if you just pick the default priority for
your action).

S



---
http://profiles.wordpress.org/users/simonwheatley/

Company information:
Sweet Interaction Ltd is Registered in England/Wales, no. 6610741
Registered office: 7 Malton Av, Manchester, M21 8AT


More information about the wp-hackers mailing list