When you reigster/addregistering a page tofor the admin area, using add_submenu_page
or any of the other add_{TYPE}_page
functions the fourth parameter accepts a unique identifier, this identifier designates the hook your givenregistered page will use..
So for a moment imagineIf i registered a page like so..were to register an options for example:
add_options_page('My 'Example Plugin Options', 'My'Example Plugin', 'manage_options', 'my'example-uniqueplugin-identifier', 'my_plugin_options''example_plugin_options' );
In the example above the unique identifier is my-unique-identifier
. Various hooks are then available for that page specifically, and here'shere are just a few possible actions for that hook.the now registered page.
// load-{HANDLE}
add_action( 'load-myexample-uniqueplugin-identifier', 'my_callback_function''example_plugin_callback' );
// admin_head-{HANDLE}
add_action( 'admin_head-myexample-uniqueplugin-identifier', 'my_callback_function''example_plugin_callback' );
// admin_print_scripts-{HANDLE}
add_action( 'admin_print_scripts-myexample-uniqueplugin-identifier', 'my_callback_function''example_plugin_callback' );
// admin_print_styles-{HANDLE}
add_action( 'admin_print_styles-myexample-uniqueplugin-identifier', 'my_callback_function''example_plugin_callback' );
function example_plugin_callback() {
// Run your code here
}
There'sThere is also the admin_enqueue_scripts
hook, which provides the name of the current hookhandle in the string/variable it passes along to callback functions that hook on..
add_action( 'admin_enqueue_scripts', 'example_plugin_callback' );
function example_plugin_callback( $handle ) {
// If the handle is not the page registered earlier, return
if( 'example-plugin-identifier' =! $handle )
return;
// Run your code here
}
You should nevernot typically need to hook ontouse admin_head
,admin_print_scripts
or admin_print_styles
unless you have a script/style you wantspecific requirement to load fortarget every admin page, if you are, then you're doing it wrong (because there's hooks for everyadministration page specifically inor perform conditional logic inside the administration area)callback to target specific registered pages.
Hope that helps..