Add Snippet To Project
If you’re looking to streamline your WordPress admin area and remove some of the clutter, you might want to consider removing some of the submenu items that you don’t use. For example, if you’re not using the default WordPress editor and instead using a page builder plugin, you might want to remove the default "Pages" submenu item. In this article, we’ll show you how to remove all admin submenu items, giving you a cleaner and more focused WordPress backend experience.
function wpturbo_remove_all_admin_submenu_items() {
global $submenu;
// unset all submenu items
foreach ( $submenu as $menu_slug => $submenu_items ) {
foreach( $submenu_items as $key => $item ) {
unset( $submenu[$menu_slug][$key] );
}
}
}
add_action( 'admin_menu', 'wpturbo_remove_all_admin_submenu_items', 999 );
The code snippet above shows how to remove all submenu items from the WordPress admin menu. This can be useful if you want to simplify the admin area for clients or users who don’t need access to certain settings or features.
The first part of the code defines a new function called wpturbo_remove_all_admin_submenu_items()
. Inside this function, we use the global
keyword to access the $submenu
variable, which contains an array of all submenu items in the WordPress admin menu.
We then use two nested foreach
loops to iterate through this array and unset all submenu items. The outer foreach
loop iterates through each top-level menu item (e.g. "Posts", "Pages", "Appearance", etc.) and the inner foreach
loop iterates through each submenu item in that menu.
foreach ( $submenu as $menu_slug => $submenu_items ) {
foreach( $submenu_items as $key => $item ) {
unset( $submenu[$menu_slug][$key] );
}
}
The unset()
function removes the submenu item from the array, effectively removing it from the WordPress admin menu.
Finally, we use the add_action()
function to add our wpturbo_remove_all_admin_submenu_items()
function to the admin_menu
action. We set the priority to 999 to make sure our function runs last and removes all submenu items after they have been added by other plugins or WordPress core.
add_action( 'admin_menu', 'wpturbo_remove_all_admin_submenu_items', 999 );
That’s it! With this code in place, all submenu items will be removed from the WordPress admin menu.