Вопрос

WordPress tries to be event driven with all it's actions and filters you can hook to. But it also has special functions to create admin pages such as add_submenu_page() which accepts a (callback) $function param.

When does this function actually execute? Between or at which action trigger time?

It must be pretty late in the action order because I've tried hooking to admin_notices from it and it doesn't work. See Actions Run During an Admin Page Request for the typical action triggering order when in /wp-admin .

More details

Why do I want to know this? I'm processing a form from the callback of add_submenu_page() because it's the best place I could think of to process the form POST data, and on validation errors I want to display a simple admin notice "the right way" which is by hooking a fn into admin_notices. I guess I could just print the notice in a conditional within my html code, or try to process the form at an earlier stage.

Note: I don't like the idea of using admin_post_(action) instead of the callback of add_submenu_page(), it's not useful.

Это было полезно?

Решение

It must be pretty late in the action order because I've tried hooking to admin_notices from it and it doesn't work.

$function callback (param of add_submenu_page) is used to output the content of admin pages, so it runs when WordPress is actually displaying the markup (body) of page.

That should be pretty clear because you use echo inside that callback.

In the Actions Run During an Admin Page Request you linked, it is just before "in_admin_footer".

"admin_notices" is also fired when WordPress output the content of admin pages, but just earlier (output of admin notices is on top of admin pages body).

If you need to conditionally output an admin notice, put a condition on "admin_notices" callback.

Другие советы

For settings pages that are located in any other location, default admin notices are not displayed automatically. So if your plugin or theme page is located in its own menu, or in a submenu of any menu other than Settings, WordPress does not automatically display default admin notices. In such case, you need to invoke settings_errors() to display the default error and updated messages. - DigWP.com

References:

  1. WordPress codex
Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top