最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - What's the latest I can hook into wp_ajax_%?

programmeradmin3浏览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?

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?

Share Improve this question edited Jun 16, 2019 at 19:00 coolpasta asked Jun 16, 2019 at 9:59 coolpastacoolpasta 9691 gold badge9 silver badges24 bronze badges 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'? – Jacob Peattie Commented Jun 16, 2019 at 10:30
  • @JacobPeattie I've added details to my question. – coolpasta Commented Jun 16, 2019 at 18:24
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论