The get_plugin_page_hookname() WordPress PHP function retrieves the hook name for the administrative page of a plugin.
Usage
get_plugin_page_hookname($plugin_page, $parent_page)
Example:
$plugin_page = 'my-custom-plugin'; $parent_page = 'options-general.php'; $hookname = get_plugin_page_hookname($plugin_page, $parent_page);
Parameters
$plugin_page(string) – The slug name of the plugin page.$parent_page(string) – The slug name for the parent menu (or the file name of a standard WordPress admin page).
More information
See WordPress Developer Resources: get_plugin_page_hookname()
Examples
Adding a Submenu Page
This example demonstrates how to use the get_plugin_page_hookname() function to add a submenu page under the ‘Settings’ menu and enqueue a stylesheet for the newly created submenu page.
function my_custom_plugin_menu() {
$plugin_page = 'my-custom-plugin';
$parent_page = 'options-general.php';
add_submenu_page($parent_page, 'My Custom Plugin', 'My Custom Plugin', 'manage_options', $plugin_page, 'my_custom_plugin_page_callback');
$hookname = get_plugin_page_hookname($plugin_page, $parent_page);
add_action("admin_print_styles-$hookname", 'my_custom_plugin_enqueue_styles');
}
add_action('admin_menu', 'my_custom_plugin_menu');
function my_custom_plugin_page_callback() {
echo '<h1>My Custom Plugin Settings</h1>';
}
function my_custom_plugin_enqueue_styles() {
wp_enqueue_style('my-custom-plugin-styles', plugin_dir_url(__FILE__) . 'css/my-custom-plugin.css');
}
Adding a Top-Level Menu Page
This example shows how to add a top-level menu page for a custom plugin and use get_plugin_page_hookname() to load a custom script only on the plugin’s admin page.
function my_custom_plugin_menu() {
$plugin_page = 'my-custom-plugin';
$parent_page = null;
add_menu_page('My Custom Plugin', 'My Custom Plugin', 'manage_options', $plugin_page, 'my_custom_plugin_page_callback', 'dashicons-admin-plugins');
$hookname = get_plugin_page_hookname($plugin_page, $parent_page);
add_action("admin_print_scripts-$hookname", 'my_custom_plugin_enqueue_scripts');
}
add_action('admin_menu', 'my_custom_plugin_menu');
function my_custom_plugin_page_callback() {
echo '<h1>My Custom Plugin Dashboard</h1>';
}
function my_custom_plugin_enqueue_scripts() {
wp_enqueue_script('my-custom-plugin-scripts', plugin_dir_url(__FILE__) . 'js/my-custom-plugin.js', array('jquery'), '1.0.0', true);
}
Changing Admin Page Title
This example demonstrates how to use the get_plugin_page_hookname() function to change the admin page title of a custom plugin page.
function my_custom_plugin_menu() {
$plugin_page = 'my-custom-plugin';
$parent_page = 'options-general.php';
add_submenu_page($parent_page, 'My Custom Plugin', 'My Custom Plugin', 'manage_options', $plugin_page, 'my_custom_plugin_page_callback');
$hookname = get_plugin_page_hookname($plugin_page, $parent_page);
add_action("admin_head-$hookname", 'my_custom_plugin_change_title');
}
add_action('admin_menu', 'my_custom_plugin