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

Similar hook to 'init' for when plugin page loads

programmeradmin0浏览0评论

Background to my question:

I have the following in my code

add_action('admin_menu', __NAMESPACE__.'\menu_item');
function menu_item() {
   add_menu_page(...);
}

From the WordPress documentation it is said that 'admin_menu' hook is called after the menu structure is in place but this doesn't seem to be the case as interrupting it results in an empty html page.

From my understanding 'admin_menu' is called for the admin environment after all admin functionality is loaded but before rendering the page similar to 'template_redirect' for posts. 'add_menu_page' sets up the menu with a link to admin.php?page=mypage executing a function when navigating to admin.php?page=mypage. By the time admin.php?page=mypage executes the page is already rendered except for the plugin code.

What I'm looking for:

Is there a hook similar to 'admin_menu' that gets called only with admin.php?page=mypage but before any html is sent?

What I'm looking to do is send my own response when certain parameters are present and only when my plugin is loaded.

EDIT: Let me clarify the question some more. 'admin_menu' loads before any html is sent but also before WordPress has determined which plugin function to load, i.e. admin.php?page=mypage. The next hook is the plugin function set up with add_menu_page() which is specific to the plugin but that only executes after the html is rendered.

Background to my question:

I have the following in my code

add_action('admin_menu', __NAMESPACE__.'\menu_item');
function menu_item() {
   add_menu_page(...);
}

From the WordPress documentation it is said that 'admin_menu' hook is called after the menu structure is in place but this doesn't seem to be the case as interrupting it results in an empty html page.

From my understanding 'admin_menu' is called for the admin environment after all admin functionality is loaded but before rendering the page similar to 'template_redirect' for posts. 'add_menu_page' sets up the menu with a link to admin.php?page=mypage executing a function when navigating to admin.php?page=mypage. By the time admin.php?page=mypage executes the page is already rendered except for the plugin code.

What I'm looking for:

Is there a hook similar to 'admin_menu' that gets called only with admin.php?page=mypage but before any html is sent?

What I'm looking to do is send my own response when certain parameters are present and only when my plugin is loaded.

EDIT: Let me clarify the question some more. 'admin_menu' loads before any html is sent but also before WordPress has determined which plugin function to load, i.e. admin.php?page=mypage. The next hook is the plugin function set up with add_menu_page() which is specific to the plugin but that only executes after the html is rendered.

Share Improve this question edited Feb 10 at 17:19 PromZA asked Feb 10 at 16:13 PromZAPromZA 11 bronze badge 6
  • I think you've confused the phrase "hook is called after the menu structure is in place" to mean after the menu HTML is in place, which is not true ( and would make inserting menu items anywhere other than the end impossible if it was ). The structure it's referring to is a data structure, aka objects/arrays/variables. What kind of response are you trying to send? Is this a redirect/auth flow of some sort? – Tom J Nowell Commented 2 days ago
  • @tom-j-nowell Yes the html. The 'admin_menu' hook is called before that but is not specific to the plugin. The hook set up with add_menu_page() is specific to the plugin but by that time the html is in place. There's a missing hook specific to the plugin but before the html is in place. – PromZA Commented 2 days ago
  • @tom-j-nowell The response is an acknowledgement to a command the plugin sends to the site and can't include any extra html. – PromZA Commented 2 days ago
  • a form handling or an AJAX request? If it's an AJAX request then this is not appropriate and you should instead use an authenticated REST API endpoint – Tom J Nowell Commented 2 days ago
  • look a this action : developer.wordpress.org/reference/hooks/load-page_hook – mmm Commented 2 days ago
 |  Show 1 more comment

1 Answer 1

Reset to default 0

admin_init runs before the page is rendered, does that help?

发布评论

评论列表(0)

  1. 暂无评论