load-(page_hook)action-hookWP 2.1.0

Fires on a particular admin page. The page on which the hook fires is specified by (page_hook).

It fires on the specified page before the page content is output—that is, before admin_print_styles, admin_print_scripts, admin_head, and admin_notices.

This hook is usually used together with the page registration functions add_menu_page(), add_submenu_page(), or derived add_*_page() functions such as add_options_page().

After registering a page, each of these functions returns its $page_hook. For example, register a page in the Options section and obtain a hook for the new page that fires only there:

add_action( 'admin_menu', 'test_load' );
function test_load() {
	$page_hook = add_options_page( 'Test', 'Test', 8, 'testload' );

	add_action( "load-$page_hook", 'my_load_function' );
	// The hook name will be: load-tools_page_testload
}

About the similar load-(pagenow) hook.

This hook is used for custom pages registered with the add_menu_page() or add_submenu_page() group of functions and their derivatives.

If the current admin page is a core page rather than a custom one, this hook will not fire. Instead, the similar load-(pagenow) hook fires. Here $pagenow is a global variable containing the current PHP file name, such as post-new.php or admin.php.

About the similar load-(plugin_page) hook.

Before running the hooks, WordPress obtains $page_hook with get_plugin_page_hook(). If that function returns no result for some reason, the similar load-(plugin_page) hook fires instead. Here plugin_page = plugin_basename( $_GET['page'] ), the current page name from the GET parameter.

Usage

add_action( 'load-(page_hook)', 'wp_kama_load_page_hook_action' );

/**
 * Function for `load-(page_hook)` action-hook.
 * 
 * @return void
 */
function wp_kama_load_page_hook_action(){

	// action...
}
$page_hook(string)

The unique identifier of the current admin page. It is used in the hook name.

For custom pages added using add_menu_page() or add_submenu_page(), a unique $page_hook is generated and returned by the registration function. It looks like:

  • toplevel_page_myplugin
  • myplugin_page_settings.

The (page_hook) name can also be obtained with get_plugin_page_hook( $plugin_page, $parent_page ).

Examples

#1 Usage example

In this example, we register a settings page and separate the actions: the page HTML is handled independently, while other actions are attached to another function. The load-(page_hook) hook makes this convenient.

Suppose our plugin must be configured. Until it is configured, we display a notice on every admin page using the admin_notices hook. However, the notice should not appear on the plugin's own settings page:

<?php
add_action('admin_menu', 'my_plugin_menu');

// Here we can check whether all plugin options are configured.
// For example, check whether the plugin options exist. If they do not,
// add a hook that creates them.
// For now, the hook is always active!
add_action( 'admin_notices', 'my_plugin_admin_notices' );

function my_plugin_menu() {
	// Register the plugin settings page and obtain its suffix
	$hook_suffix = add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');

	// Use the suffix to perform actions only on our page
	add_action( 'load-' . $hook_suffix , 'my_load_function' );
}

function my_load_function() {
	// Everything here runs only on the plugin settings page, so hide the notice by removing the hook added above.
	remove_action( 'admin_notices', 'my_plugin_admin_notices' );
}

function my_plugin_admin_notices() {
	echo "<div id='notice' class='updated fade'><p>You have not configured the plugin. Please do so!</p></div>\n";
}
// Plugin settings page
function my_plugin_options() {
	if( ! current_user_can('manage_options') )  {
		wp_die( __('You do not have sufficient permissions to access this page.') );
	}
	echo '<div class="wrap">';
	echo '<p>The settings HTML goes here.</p>';
	echo '</div>';
}
?>

#2 Load a plugin script only on its pages

add_action( 'admin_init', 'my_plugin_admin_init' );
add_action( 'admin_menu', 'my_plugin_admin_menu' );

function my_plugin_admin_init() {
	/* Register our script. */
	wp_register_script( 'my-plugin-script', plugins_url('/script.js', __FILE__) );
}

function my_plugin_admin_menu() {
	/* Register our plugin page */
	$page = add_submenu_page(
		'edit.php', // Parent menu page
		__( 'My plugin', 'myPlugin' ), // Menu item name
		__( 'My plugin', 'myPlugin' ), // Page heading
		'manage_options', // Capability that determines access to the menu item
		'my_plugin-options', // Plugin page slug (part of its address)
		'my_plugin_manage_menu' // Function that outputs the page
	);

	/* Use the registered page to load the script */
	add_action( "admin_print_scripts-{$page}", 'my_plugin_admin_scripts' );
}

function my_plugin_admin_scripts() {
	/*
	 * This function is called only on the plugin page; enqueue our script
	 */
	wp_enqueue_script( 'my-plugin-script' );
}

function my_plugin_manage_menu() {
	/* Output the plugin page */
}

Changelog

Since 2.1.0 Introduced.

Where the hook is called

In file: /wp-admin/admin.php
load-(page_hook)
wp-admin/admin.php 242
do_action( "load-{$page_hook}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

Where the hook is used in WordPress

wp-admin/includes/admin-filters.php 130
add_action( 'load-plugins.php', 'wp_plugin_update_rows', 20 ); // After wp_update_plugins() is called.
wp-admin/includes/admin-filters.php 131
add_action( 'load-themes.php', 'wp_theme_update_rows', 20 ); // After wp_update_themes() is called.
wp-admin/includes/class-custom-background.php 81
add_action( "load-{$page}", array( $this, 'admin_load' ) );
wp-admin/includes/class-custom-background.php 82
add_action( "load-{$page}", array( $this, 'take_action' ), 49 );
wp-admin/includes/class-custom-background.php 83
add_action( "load-{$page}", array( $this, 'handle_upload' ), 49 );
wp-includes/default-filters.php 699
add_action( 'load-post.php', 'wp_set_up_cross_origin_isolation' );
wp-includes/default-filters.php 700
add_action( 'load-post-new.php', 'wp_set_up_cross_origin_isolation' );
wp-includes/default-filters.php 701
add_action( 'load-site-editor.php', 'wp_set_up_cross_origin_isolation' );
wp-includes/default-filters.php 702
add_action( 'load-widgets.php', 'wp_set_up_cross_origin_isolation' );
wp-includes/update.php 1194
add_action( 'load-plugins.php', 'wp_update_plugins' );
wp-includes/update.php 1195
add_action( 'load-update.php', 'wp_update_plugins' );
wp-includes/update.php 1196
add_action( 'load-update-core.php', 'wp_update_plugins' );
wp-includes/update.php 1200
add_action( 'load-themes.php', 'wp_update_themes' );
wp-includes/update.php 1201
add_action( 'load-update.php', 'wp_update_themes' );
wp-includes/update.php 1202
add_action( 'load-update-core.php', 'wp_update_themes' );