template_redirect not being called when using ajax

Usually I hook all my custom plugins code with the template_redirect hook but one of the issue I encountered is it does not work for the ajax call.

So if I want to target both standard calls and ajax call what hook should I use?

Is wp_loaded a good choice?

1 Answer
1

For AJAX you need to hook into the wp_ajax_{action} hook

Specifically,

add_action('wp_ajax_my_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

See http://codex.wordpress.org/AJAX_in_Plugins

Leave a Comment