Add Snippet To Project
WordPress provides a way for developers to add pages and menus to the admin dashboard through the use of hooks and the add_menu_page()
function. However, sometimes it can be difficult to keep track of which menu page is being displayed, especially when debugging or developing a plugin or theme. In this article, we’ll show you how to display the hook_suffix
as an admin notice, providing a quick and easy way to identify which menu page is currently being viewed.
function wpturbo_display_hook_suffix() {
$hook_suffix = get_current_screen()->id;
?>
<div class="notice notice-info is-dismissible">
<p><?php _e( 'The current hook suffix is: ', 'wpturbo' ); ?><strong><?php echo esc_html( $hook_suffix ); ?></strong></p>
</div>
<?php
}
add_action( 'admin_notices', 'wpturbo_display_hook_suffix' );
In this article, we will talk about how to display a hook suffix in an admin notice in WordPress. The hook suffix is a unique identifier for a specific page or screen in the admin area of WordPress. Knowing the hook suffix can be useful for developers when debugging or troubleshooting code that is specific to a certain page or screen.
The code snippet above defines a new function called wpturbo_display_hook_suffix()
. This function will retrieve the current hook suffix and display it in an admin notice on the WordPress dashboard.
To get the hook suffix, we use the get_current_screen()->id
method, which will return the unique identifier for the current page or screen being displayed in the WordPress admin area.
Inside the function, we include HTML markup to structure the admin notice. We create a div
with a class of notice notice-info is-dismissible
, which applies the appropriate styling for an informational notice in the WordPress admin dashboard.
?>
<div class="notice notice-info is-dismissible">
<p><?php _e( 'The current hook suffix is: ', 'wpturbo' ); ?><strong><?php echo esc_html( $hook_suffix ); ?></strong></p>
</div>
<?php
We use the WordPress _e()
function to output the text "The current hook suffix is:" in the main body of the admin notice, which is followed by a strong
tag containing the actual hook suffix.
Finally, we hook the wpturbo_display_hook_suffix()
function into the admin_notices
action. This ensures that the admin notice will be displayed on the WordPress dashboard whenever WordPress renders its admin notices.
That's it! You can copy and paste this code into your own WordPress functions file to display the hook suffix in your own admin notices.