Removing Menu Items in WordPress Admin Panel

When creating a blog, it is advisable to restrict access to certain functions. This will help avoid many problems, including accidental deletion of the main theme. To do this, it is necessary to implement code in the existing theme functions.php file. It should be noted that removing menu items does not prohibit direct access to them (access by link), but simply visually removes the menu items:

add_action( 'admin_menu', 'remove_admin_menus' );
function remove_admin_menus(){
	global $menu;

	$unset_titles = [
		__( 'Dashboard' ),
		__( 'Posts' ),
		__( 'Media' ),
		__( 'Links' ),
		__( 'Pages' ),
		__( 'Appearance' ),
		__( 'Tools' ),
		__( 'Users' ),
		__( 'Settings' ),
		__( 'Comments' ),
		__( 'Plugins' ),
	];

	end( $menu );
	while( prev( $menu ) ){

		$value = explode( ' ', $menu[ key( $menu ) ][0] );
		$title = $value[0] ?: '';

		if( in_array( $title, $unset_titles, true ) ){
			unset( $menu[ key( $menu ) ] );
		}
	}

}

It is necessary to explain that:

  • __('Dashboard') — main admin page (dashboard);
  • __('Posts') — "Posts" menu item;
  • __('Media') — "Media" menu item (images, video, etc.);
  • __('Links') — unnecessary "Links" menu item;
  • __('Pages') — "Pages" menu item;
  • __('Appearance') — "Appearance" menu item;
  • __('Tools') — "Tools" menu item, where there are various things like "import", "export";
  • __('Users') — users;
  • __('Settings') — "Settings" menu item. It can be closed for clients, otherwise they will configure it...;
  • __('Comments') — comments;
  • __('Plugins') — and finally, the sacred: "Plugins" menu item.

Also, there is a special WP function for removing menu and submenu items: remove_menu_page() and remove_submenu_page().