The load-{$plugin_page} WordPress PHP action fires before a particular plugin screen is loaded. This hook is for plugin screens where the file to load is directly included, rather than using a function. The dynamic portion of the hook name, $plugin_page, refers to the plugin basename.
Usage
add_action('load-{plugin_basename}', 'your_custom_function');
function your_custom_function() {
// your custom code here
}
Parameters
- $plugin_page (string): The plugin basename, which is the dynamic part of the hook name.
More information
See WordPress Developer Resources: load-{$plugin_page}
Examples
Prevent access to a specific plugin page
Prevent access to a specific plugin page based on user capabilities.
add_action('load-my_plugin_page', 'restrict_access_to_plugin_page');
function restrict_access_to_plugin_page() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
}
Enqueue custom CSS and JS for a plugin page
Enqueue custom CSS and JS files only when a specific plugin page is being loaded.
add_action('load-custom_plugin_page', 'enqueue_custom_assets');
function enqueue_custom_assets() {
wp_enqueue_style('custom-plugin-css', 'path/to/custom-plugin.css');
wp_enqueue_script('custom-plugin-js', 'path/to/custom-plugin.js');
}
Add a custom meta box to a plugin page
Add a custom meta box to a specific plugin page.
add_action('load-my_plugin_page', 'add_custom_meta_box');
function add_custom_meta_box() {
add_meta_box('custom-meta-box', 'Custom Meta Box', 'custom_meta_box_callback', 'my_plugin_page', 'normal', 'high');
}
function custom_meta_box_callback($post) {
// your custom code here
}
Modify screen options for a plugin page
Modify the screen options for a specific plugin page.
add_action('load-custom_plugin_page', 'modify_screen_options');
function modify_screen_options() {
$screen = get_current_screen();
$screen->add_option('custom_option', 'Custom option description');
}
Execute custom code when a specific plugin page is loaded
Execute custom code only when a specific plugin page is being loaded.
add_action('load-custom_plugin_page', 'execute_custom_code');
function execute_custom_code() {
// your custom code here
}