admin_menu
Fires before the admin menu is displayed. Use it to add your own admin pages or remove menu items.
When it fires
admin_menu runs on every admin page load, after WordPress has set up the default menu items and before the menu is displayed. Call add_menu_page(), add_submenu_page() or remove_menu_page() from here.
The current user is known, so you can check capabilities. In multisite, the network admin uses network_admin_menu instead.
Parameters
$contextstring |
Empty context. |
|---|
Example: Hide menu items from non-administrators
add_action( 'admin_menu', function () {
if ( current_user_can( 'manage_options' ) ) {
return;
}
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit-comments.php' );
}, 999 );
Good to know
- Removing a menu item only hides the link. The screen still opens if someone types its URL, so rely on capabilities to restrict access.
- Use a late priority, such as 999, so plugins have added their items before you remove them.
- Pass remove_menu_page() the file name from the menu link, such as 'tools.php', or the page slug for plugin pages.