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;
}

Plugin Shoutout : Advanced Custom Fields

Like most developers, I have a tendency to want to code everything from scratch. I suppose like any craftsman, there’s a resistance to relying on a machine to do work you enjoy doing with your hands. But sometimes the challenges of a particular project force your hand and you need to save time on meta boxes and custom fields and spend more time on other aspects of the project. Lately I’ve been relying on Advanced Custom Fields for managing custom fields and I’ve been very impressed.

The interface is smooth and as far as I can tell so far, there’s no front end performance hit. Though, I must admit I do not use the front-end functions provided by ACF and instead prefer to rely on the standard <code>get_post_meta()</code> approach.

But there are two things specifically I enjoy about this plugin. First, it gives you the option to have your “field group” either inside an expandable meta box. This is the standard way custom fields are presented. But it also gives you the ability to add the fields right into the form with no meta box wrapping element. This gives your form some variety. (see screen shots)

The second thing I really enjoy about this plugin is the API for adding custom field types. Developers always love finding an organized API when they need one. For this specific project I need a field where the user could select a NextGen Gallery from a dropdown. All I had to do was create a class extending the acfField. Then add the following to my functions.php:

add_action('acf_register_field','ngg_register_acf_field');
function ngg_register_acf_field() {
add_filter('acf_register_field','ngg_field',0,1);
}

function ngg_field($custom) {
global $acf;
$ngg = array('class'=>'acf_NGG_Gallery','url'=>PATHTODIR.'/acf_ngg.php');
$custom['ngg'] = $ngg;
return $custom;
   }