Using WordPress ‘get_plugin_page_hook()’ PHP function

The get_plugin_page_hook() WordPress PHP function retrieves the hook attached to the administrative page of a plugin.

Usage

$hook_name = get_plugin_page_hook( $plugin_page, $parent_page );

Input:

  • $plugin_page: The slug name of the plugin page.
  • $parent_page: The slug name for the parent menu (or the file name of a standard WordPress admin page).

Output:

  • Returns the hook name for the plugin page or false if the hook does not exist.

Parameters

  • $plugin_page (string) – The slug name of the plugin page. This is required.
  • $parent_page (string) – The slug name for the parent menu (or the file name of a standard WordPress admin page). This is required.

More information

See WordPress Developer Resources: get_plugin_page_hook()

Examples

Get the hook name for a plugin’s admin page

Retrieve the hook name for the ‘example-plugin-settings’ admin page under the ‘settings’ menu.

$plugin_page = 'example-plugin-settings';
$parent_page = 'options-general.php';
$hook_name = get_plugin_page_hook( $plugin_page, $parent_page );

Display a message if the plugin page hook exists

Check if the plugin page hook exists, and display a message accordingly.

$plugin_page = 'custom-plugin-page';
$parent_page = 'plugins.php';
$hook_name = get_plugin_page_hook( $plugin_page, $parent_page );

if ( $hook_name ) {
    echo 'The plugin page hook exists: ' . $hook_name;
} else {
    echo 'The plugin page hook does not exist.';
}

Remove an action from a plugin page

If the plugin page hook exists, remove a specified action from it.

$plugin_page = 'my-plugin-settings';
$parent_page = 'options-general.php';
$hook_name = get_plugin_page_hook( $plugin_page, $parent_page );

if ( $hook_name ) {
    remove_action( $hook_name, 'some_action_function' );
}

Add an action to a plugin page

If the plugin page hook exists, add a specified action to it.

$plugin_page = 'example-plugin-page';
$parent_page = 'tools.php';
$hook_name = get_plugin_page_hook( $plugin_page, $parent_page );

if ( $hook_name ) {
    add_action( $hook_name, 'custom_action_function' );
}

Check if a specific hook is attached to a plugin page

Determine if the ‘admin_enqueue_scripts’ hook is attached to the ‘sample-plugin-page’ admin page.

$plugin_page = 'sample-plugin-page';
$parent_page = 'options-general.php';
$hook_name = get_plugin_page_hook( $plugin_page, $parent_page );

if ( $hook_name && has_action( $hook_name, 'admin_enqueue_scripts' ) ) {
    echo 'The admin_enqueue_scripts hook is attached to the plugin page.';
} else {
    echo 'The admin_enqueue_scripts hook is not attached to the plugin page.';
}