load-(pagenow)
Fires when a particular admin page loads.
This event fires before the page HTML is output and is suitable for running code while a particular admin page loads, such as access checks, submitted form processing, and other preparatory actions.
This is a dynamic event: (pagenow) is replaced with the value of the global $pagenow variable for the required page.
There is a similar load-(page_hook) hook. The difference is:
-
load-(pagenow)— for standard core admin pages such asedit.phpandpost-new.php. Based on the$pagenowvariable. load-(page_hook)— for plugin pages created with add_menu_page() or add_submenu_page(). Based on the$hookvalue returned when the page is added.
Use the following code to determine the $pagenow value on the current page:
die( $GLOBALS['pagenow'] );
Usage
add_action( 'load-(pagenow)', 'wp_kama_load_pagenow_action' );
/**
* Function for `load-(pagenow)` action-hook.
*
* @return void
*/
function wp_kama_load_pagenow_action(){
// action...
}
- $pagenow(string)
Global variable containing the name of the current PHP file loaded in the admin area.
Hook values for different admin pages:
load-index.php— main Dashboard page.load-edit.php— posts list.load-post.php— edit a post.load-post-new.php— create a new post.load-edit-tags.php— manage tags and categories.load-upload.php— Media Library.load-media-new.php— upload a new media file.load-edit-comments.php— comments.load-themes.php— themes.load-customize.php— Customizer.load-widgets.php— widgets.load-nav-menus.php— navigation menus.load-plugins.php— plugins list.load-plugin-install.php— install plugins.load-users.php— users list.load-user-new.php— add a new user.load-profile.php— current user profile.load-edit.php— pages list.load-edit.php— custom post type list.load-options-general.php— General Settings.load-options-writing.php— Writing Settings.load-options-reading.php— Reading Settings.load-options-discussion.php— Discussion Settings.load-options-media.php— Media Settings.load-options-permalink.php— Permalink Settings.load-tools.php— Tools.load-import.php— Import.load-export.php— Export.load-admin.php— plugin and custom pages.
Examples
#1 Process data when settings are saved
Process data when a form is submitted on a particular settings page.
add_action( 'load-options-general.php', 'handle_my_settings_save' );
function handle_my_settings_save() {
if ( isset( $_POST['my_setting'] ) ) {
update_option( 'my_setting', sanitize_text_field( $_POST['my_setting'] ) );
}
}
#2 Register a script only on the post editing page
Enqueue a script only when the post editing page (post.php) loads.
add_action( 'load-post.php', 'my_admin_script_loader' );
function my_admin_script_loader() {
wp_enqueue_script( 'my-script', plugin_dir_url( __FILE__ ) . 'script.js', [], '1.0', true );
}Changelog
| Since 2.1.0 | Introduced. |
Where the hook is called
do_action( "load-{$pagenow}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
Where the hook is used in WordPress
add_action( 'load-plugins.php', 'wp_plugin_update_rows', 20 ); // After wp_update_plugins() is called.
add_action( 'load-themes.php', 'wp_theme_update_rows', 20 ); // After wp_update_themes() is called.
add_action( "load-{$page}", array( $this, 'admin_load' ) );
add_action( "load-{$page}", array( $this, 'take_action' ), 49 );
add_action( "load-{$page}", array( $this, 'handle_upload' ), 49 );
add_action( 'load-post.php', 'wp_set_up_cross_origin_isolation' );
add_action( 'load-post-new.php', 'wp_set_up_cross_origin_isolation' );
add_action( 'load-site-editor.php', 'wp_set_up_cross_origin_isolation' );
add_action( 'load-widgets.php', 'wp_set_up_cross_origin_isolation' );
add_action( 'load-plugins.php', 'wp_update_plugins' );
add_action( 'load-update.php', 'wp_update_plugins' );
add_action( 'load-update-core.php', 'wp_update_plugins' );
add_action( 'load-themes.php', 'wp_update_themes' );
add_action( 'load-update.php', 'wp_update_themes' );
add_action( 'load-update-core.php', 'wp_update_themes' );