Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

admin_menu

Fires before the admin menu is displayed. Use it to add your own admin pages or remove menu items.

  • Type Action
  • From WordPress
  • Since 1.5.0
  • Fired in wp-admin/includes/menu.php

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

Parameters
$contextstring Empty context.

Example: Hide menu items from non-administrators

PHP
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.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.