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.

  • Summarizing, you give add_submenu_page() a callback function to display the admin screen. I'm also using that callback to process the POST and hook to admin_notices, which doesn't work at that point. So my Q really is when does the callback sent to add_submenu_page() is invoked by WordPress. Thanks, I'll update the question. – Jorge Orpinel Oct 9 '14 at 17:21
up vote 0 down vote accepted

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.

  • I like this answer (can't vote up yet due to nil reputation). Do you have any reference or know a way to check when exactly does WordPress output html in the action triggering sequence? I guess that's the underlying question here. – Jorge Orpinel Oct 9 '14 at 17:48
  • p.s. this guide has a nice diagram called "Make sense of WP core load" but it's still unclear to me from it where exactly does WP output html. code.tutsplus.com/articles/… – Jorge Orpinel Oct 9 '14 at 17:49
  • @JorgeOrpinel that diagram comes from this site, it's by our moderator Rarst, – gmazzap Oct 9 '14 at 17:52
  • @JorgeOrpinel the output of the html starts when WP include admin-header.php – gmazzap Oct 9 '14 at 17:54
  • @g-m Thanks! That makes sense :) – Jorge Orpinel Oct 9 '14 at 18:11

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

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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