2

At some point between muplugins_loaded and shutdown

wp_ajax_*,

wp_ajax_nopriv_* are executed, but where/when exactly?

I have found that this is working:

add_action(        'wp_ajax_actionname', 'callbackname');
add_action( 'wp_ajax_nopriv_actionname', 'callbackname);

as well as:

add_action('wp_loaded', function(){
  add_action(        'wp_ajax_actionname', 'callbackname');
  add_action( 'wp_ajax_nopriv_actionname', 'callbackname);
});

So it is after wp_loaded, but when exactly?

This isn't pure curiosity, the alternative wording of this question would be "can we safely assume that wp_ajax hooks aren't executed when Wordpress loading, including it's plugins, is incomplete"?

1 Answer 1

3

They're not triggered in the normal WordPress lifecycle. They're only triggered when you make a request to admin-ajax.php, and they're triggered as the last action before shutdown. You can see this in the file itself: https://github.com/WordPress/WordPress/blob/master/wp-admin/admin-ajax.php#L188

1
  • It's literally right before wp_die( '0' ); . So that's when the request is almost completely done. Commented Jun 7, 2023 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.