Add Snippet To Project
As a WordPress user, have you ever wished you could rearrange or customize the menu order on the WordPress admin dashboard? It can be frustrating to have to navigate through menus and submenus to find the screen you’re looking for. Fortunately, WordPress allows you to customize the admin menu order to better suit your needs. In this article, we’ll show you how to rearrange and customize the menus on your WordPress admin dashboard.
function wpturbo_custom_admin_menu_order() {
global $menu;
$menu_items = array(
'index.php', // Dashboard
'edit.php?post_type=page', // Pages
'edit.php', // Posts
'separator1', // First separator
'upload.php', // Media
'edit-comments.php', // Comments
'separator2', // Second separator
'themes.php', // Appearance
'plugins.php', // Plugins
'users.php', // Users
'tools.php', // Tools
'options-general.php', // Settings
);
$new_menu = array();
foreach ( $menu_items as $menu_item ) {
foreach ( $menu as $key => $value ) {
if ( $menu_item == $value[2] ) {
$new_menu[$key] = $value;
}
}
}
$menu = $new_menu;
}
add_action( 'admin_menu', 'wpturbo_custom_admin_menu_order' );
In this tutorial, we will show you how to customize the order of the WordPress admin menu using PHP code.
We achieve this by first defining the new order of the menu in an array called $menu_items
. This needs to be a list of strings where each string represents a menu item. You can use the WordPress built-in query strings for post types, taxonomies, and other WordPress features to define these strings.
Next, we create a new array called $new_menu
which will contain the new order of the menu items. We loop through each menu item in $menu_items
and check if it exists in the $menu
array, which contains the existing menu items.
If the menu item exists in $menu
, we add it to the $new_menu
array. We add the menu item to the $new_menu
array using the menu item index as the key, and menu item data as the value.
Finally, we replace the existing $menu
array with our new $new_menu
array. We do this using the assignment operator. This is possible because $menu
is a global variable that stores the menu for the current screen.
To hook this custom admin menu order function into WordPress, we add the add_action()
function. We use the admin_menu
hook to make sure our function is executed when the admin menu is loaded. This ensures that we have access to $menu
so we can customize the menu order.