0

It seems that there's a dispatch on shutdown, so, it seems that wp_ajax_my_action is executed at the very last of the request but I can't be sure since it doesn't show in the normal actions viewer.

What's the latest I can hook into it?

Not sure why this created confusion, but I meant that, obviously, if you do wp_ajax_% when you are in the footer, it's not gonna work. If you do it close to init, say in functions.php, it works, the hooking happens so it seems that before WP does the AJAX call, which calls wp_die() anyways, it sets up the hooks and there is a point after which you can no longer hook into these endpoints and there-in lies my question: when exactly is it too late to hook a function to a wp_ajax_ endpoint?

Example:

add_action( 'wp_ajax_my_endpoint', 'my_function' ) in functions.php works.

add_action( 'wp_ajax_my_endpoint', 'my_function' ) in footer.php does not work.

How so?

2
  • WordPress runs wp_die() as soon as the AJAX hook is run. So it's the last thing to happen. But why would you be hooking it 'late'? Commented Jun 16, 2019 at 10:30
  • @JacobPeattie I've added details to my question.
    – coolpasta
    Commented Jun 16, 2019 at 18:24

1 Answer 1

2

There seems to be a big misunderstanding here about how hooks work, and how AJAX works.

The reason that you can't hook into AJAX in footer.php is because footer.php is not loaded during an AJAX request. No templates are.

When you run add_action() you are scheduling a given function to fire when that action is fired during the current request. When you load a page, WordPress loads functions.php and runs all the add_action() functions immediately so that all the functions are queued up to fire whenever the actions are triggered by anything that comes later. It does this for every single page load. At no stage are any hooks 'stored' so that they can be run on a separate request.

This brings us to AJAX. An AJAX request, made by jQuery or whatever, is a separate request. It's the equivalent of opening a link in a new tab, it just happens in the background. This means that nothing that happened during the original page load is scheduled to run during that AJAX request. The only way to schedule something to run when the AJAX request is made is to schedule it in functions.php or a plugin, so that the action is scheduled when WordPress loads again for that request.

Because hooked functions aren't stored between requests, any function that's hooked in a template can only run if the action it's hooked to occurs in a later template. With AJAX requests, though, no templates are loaded, because it's a background request, not a request for a specific page.

4
  • Very informative and that was clear, obviously, I was trying to make a point, but, still, what is the latest hook that I can use? It seems to be wp_loaded, looking at admin-ajax.php.
    – coolpasta
    Commented Jun 17, 2019 at 13:55
  • Use for what? Your add_action for AJAX shouldn’t need to be in a hook. Commented Jun 17, 2019 at 14:04
  • Correct, but depending on where you type it, your environment (the hook you're running on) changes. You might be running deep into a file hierarchy not realizing that your add_action( 'wp_ajax...' is running on another action than it would in functions.php.
    – coolpasta
    Commented Jun 17, 2019 at 15:08
  • You've lost me. Commented Jun 18, 2019 at 0:41

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.