The menu_order filter in WordPress allows you to customize the order of administration menu items. You can modify the menu order to display important menu items at the top of the dashboard, making it easier for users to navigate your site.
Usage
First, you need to enable custom menu ordering by adding the following code to your functions.php
file:
add_filter( 'custom_menu_order', '__return_true' );
After enabling custom menu ordering, you can modify the order of menu items using the menu_order
filter. Here’s a basic example:
function my_custom_menu_order( $menu_order ) { // your custom code here return $menu_order; } add_filter( 'menu_order', 'my_custom_menu_order' );
In the example code, the my_custom_menu_order
function takes an array of menu items as input and returns the same array after making any necessary modifications. You can modify the order of menu items in any way you like, such as moving a specific menu item to the top of the dashboard.
Parameters
$menu_order
(array): An ordered array of menu items.
More Information
When filtering the order array, any menus that are not mentioned in the array will be sorted after ones that are mentioned. Unmentioned menus are sorted in their usual order, relative to other unmentioned menus.
For more information on the menu_order
filter, check out the WordPress Developer Resources.
Examples
Move a specific menu item to the top
This example demonstrates how to move the “Posts” menu item to the top of the dashboard.
function my_custom_menu_order( $menu_order ) { // Move "Posts" menu item to the top $menu_order[] = 'edit.php'; unset( $menu_order['edit.php'] ); return $menu_order; } add_filter( 'menu_order', 'my_custom_menu_order' ); add_filter( 'custom_menu_order', '__return_true' );
In this code, we add the “edit.php” item to the end of the array using $menu_order[]
, then remove the original “edit.php” item using unset()
. This effectively moves the “Posts” menu item to the top of the dashboard.
Group menu items together
This example demonstrates how to group menu items together in the dashboard.
function my_custom_menu_order( $menu_order ) { // Move "Posts" and "Pages" menu items to top level $menu_order[] = 'edit.php'; unset( $menu_order['edit.php'] ); $menu_order[] = 'edit.php?post_type=page'; unset( $menu_order['edit.php?post_type=page'] ); // Create a submenu item for "Plugins" and "Themes" $submenu_items = array( 'plugins.php' => array(), 'themes.php' => array(), ); $menu_order['options-general.php'] = $submenu_items; return $menu_order; } add_filter( 'menu_order', 'my_custom_menu_order' ); add_filter( 'custom_menu_order', '__return_true' );
In this code, we first move the “Posts” and “Pages” menu items to the top level of the dashboard. Then we create a submenu item for the “Plugins” and “Themes” menu items under “Settings”. We accomplish this by adding a new key to the $menu_order
array with the value of the submenu items.
Remove a menu item
This example demonstrates how to remove a menu item from the dashboard.
function my_custom_menu_order( $menu_order ) { // Remove "Appearance" menu item unset( $menu_order['themes.php'] ); return $menu_order; } add_filter( 'menu_order', 'my_custom_menu_order' ); add_filter( 'custom_menu_order', '__return_true' );
In this code, we remove the “Appearance” menu item by unsetting its key in the `$menu_order` array.
Add a custom menu item
This example demonstrates how to add a custom menu item to the dashboard.
function my_custom_menu_order( $menu_order ) { // Add "Custom Item" menu item $menu_order[] = 'my-custom-item'; return $menu_order; } add_filter( 'menu_order', 'my_custom_menu_order' ); add_filter( 'custom_menu_order', '__return_true' );
In this code, we add a new menu item called “Custom Item” by appending the string “my-custom-item” to the end of the $menu_order
array.