Skip to main content
Wording/code tweaks, more example code
Source Link
t31os
  • 18.7k
  • 3
  • 56
  • 68

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..

When you reigster/add a page to 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 given page will use..

So for a moment imagine i registered a page like so..

add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');

In the example above the unique identifier is my-unique-identifier. Various hooks are then available for that page specifically, and here's a few possible actions for that hook..

add_action( 'load-my-unique-identifier', 'my_callback_function' );
add_action( 'admin_head-my-unique-identifier', 'my_callback_function' );
add_action( 'admin_print_scripts-my-unique-identifier', 'my_callback_function' );
add_action( 'admin_print_styles-my-unique-identifier', 'my_callback_function' );

There's also admin_enqueue_scripts which provides the name of the current hook in the string/variable it passes along to functions that hook on..

You should never need to hook onto admin_head,admin_print_scripts or admin_print_styles unless you have a script/style you want to load for every admin page, if you are, then you're doing it wrong (because there's hooks for every page specifically in the administration area).

Hope that helps..

When registering a page for 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 registered page will use..

If i were to register an options for example:

add_options_page( 'Example Plugin Options', 'Example Plugin', 'manage_options', 'example-plugin-identifier', 'example_plugin_options' );

Various hooks are then available for that page specifically, here are just a few possible actions for the now registered page.

// load-{HANDLE}
add_action( 'load-example-plugin-identifier', 'example_plugin_callback' );
// admin_head-{HANDLE}
add_action( 'admin_head-example-plugin-identifier', 'example_plugin_callback' );
// admin_print_scripts-{HANDLE}
add_action( 'admin_print_scripts-example-plugin-identifier', 'example_plugin_callback' );
// admin_print_styles-{HANDLE}
add_action( 'admin_print_styles-example-plugin-identifier', 'example_plugin_callback' );

function example_plugin_callback() {
    
    // Run your code here
    
}

There is also the admin_enqueue_scripts hook, which provides the name of the current handle in the string/variable it passes along to callback functions.

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 not typically need to use admin_head,admin_print_scripts or admin_print_styles unless you have a specific requirement to target every administration page or perform conditional logic inside the callback to target specific registered pages.

Hope that helps..

Source Link
t31os
  • 18.7k
  • 3
  • 56
  • 68

When you reigster/add a page to 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 given page will use..

So for a moment imagine i registered a page like so..

add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');

In the example above the unique identifier is my-unique-identifier. Various hooks are then available for that page specifically, and here's a few possible actions for that hook..

add_action( 'load-my-unique-identifier', 'my_callback_function' );
add_action( 'admin_head-my-unique-identifier', 'my_callback_function' );
add_action( 'admin_print_scripts-my-unique-identifier', 'my_callback_function' );
add_action( 'admin_print_styles-my-unique-identifier', 'my_callback_function' );

There's also admin_enqueue_scripts which provides the name of the current hook in the string/variable it passes along to functions that hook on..

You should never need to hook onto admin_head,admin_print_scripts or admin_print_styles unless you have a script/style you want to load for every admin page, if you are, then you're doing it wrong (because there's hooks for every page specifically in the administration area).

Hope that helps..