Using WordPress ‘load-{$pagenow}’ PHP action

The load-{$pagenow} WordPress PHP action fires before a particular screen is loaded, making it useful for running custom code for specific admin screens.

Usage

add_action('load-{pagenow}', 'your_custom_function');

function your_custom_function() {
    // Your custom code here
}

Parameters

  • $pagenow (string) – The filename of the current admin screen (e.g., ‘admin.php’, ‘post-new.php’, etc.).

More information

See WordPress Developer Resources: load-{$pagenow}

Examples

Redirect users from the ‘post-new.php’ screen

Prevent users from accessing the ‘Add New Post’ screen and redirect them to the ‘All Posts’ screen.

add_action('load-post-new.php', 'redirect_to_all_posts');

function redirect_to_all_posts() {
    wp_redirect(admin_url('edit.php'));
    exit;
}

Add custom JavaScript to ‘post.php’ screen

Add a custom JavaScript file to the ‘Edit Post’ screen.

add_action('load-post.php', 'enqueue_custom_js');

function enqueue_custom_js() {
    wp_enqueue_script('custom-js', plugins_url('custom.js', __FILE__));
}

Run a function on ‘themes.php’ screen

Execute a custom function when the ‘Themes’ screen is loaded.

add_action('load-themes.php', 'run_on_themes_screen');

function run_on_themes_screen() {
    // Your custom code here
}

Display a custom message on ‘plugins.php’ screen

Show a custom admin notice on the ‘Plugins’ screen.

add_action('load-plugins.php', 'show_custom_message');

function show_custom_message() {
    add_action('admin_notices', 'display_custom_message');
}

function display_custom_message() {
    echo '<div class="notice notice-info is-dismissible"><p>Your custom message goes here.</p></div>';
}

Add custom CSS to ‘users.php’ screen

Add custom CSS to the ‘Users’ screen.

add_action('load-users.php', 'enqueue_custom_css');

function enqueue_custom_css() {
    wp_enqueue_style('custom-css', plugins_url('custom.css', __FILE__));
}