WordPress Hack: Reorder the Admin Menu

For many small business clients pages wind up being the most important WordPress post type. Many of them don’t even want a blog, just a nice looking site where they can update the content periodically, i.e. the contact page, the services page etc.

So for these clients it’s often a little confusing to have “Posts” at the top of the nav menu. Here’s how you would reorder to the nav to put pages on top.

First turn on the custom ordering option:

add_filter('custom_menu_order','__return_true');

Then create a function to filter the menu order array. In the function below we’re using array_splice to cut out the menu starting at position 2 because the “posts” menu item is #3 by default. How did I know that? Just add print_r($menu) and you’ll get a dump of the menu order. After splicing the array, we iterate over the old menu array, the part we extracted ($old), using a foreach loop adding the old menu items back in.

function mc_menu_order($menu) {
// more on array_splice here: https://php.net/manual/en/function.array-splice.php
$old = array_splice($menu,2,count($menu),array('2'=>'edit.php?post_type=page'));
$keys = array_flip($menu);
foreach($old as $item) {
if(!array_key_exists($item,$keys)) {
$menu[] = $item;
}
}
unset($old);
unset($keys);
return $menu;
}

Then just add the api call to insert that function:

add_filter('menu_order','mc_menu_order');

Here it is all together:

//menu reorder
add_filter('custom_menu_order','__return_true');
add_filter('menu_order','mc_menu_order');
function mc_menu_order($menu) {
$old = array_splice($menu,2,count($menu),array('2'=>'edit.php?post_type=page'));
$keys = array_flip($menu);
foreach($old as $item) {
if(!array_key_exists($item,$keys)) {
$menu[] = $item;
}
}
unset($old);
unset($keys);
return $menu;
}