Adding a Separator to the WordPress Admin Menu

Custom Post Types in WordPress are superb! More often that not, websites that we build for clients have more than one custom post type which inevitably will clutter up the admin menu slightly. The solution to the problem is one that WordPress itself employs: De-clutter the meny by inserting a separator in between sections.

Doing this requires just a couple of simple PHP functions that you can add to your functions.php file. One to create the separator, one to add  any number of separators at any position in the menu and finally an action hook into the menu to add it.

Creating the Admin Menu Separator

The following code is used to create a function that creates the separator itself. Note that the position of the separator in the menu is passed as a variable into the function so that the function can be repeated for more than one separator, at any position.

/**
 * Create Admin Menu Separator
 **/
function add_admin_menu_separator($position) {

	global $menu;
	$index = 0;

	foreach($menu as $offset => $section) {
		if (substr($section[2],0,9)=='separator')
		    $index++;
		if ($offset>=$position) {
			$menu[$position] = array('','read',"separator{$index}",'','wp-menu-separator');
			break;
	    }
	}

	ksort( $menu );
}

Adding the Separators

The next bit of code creates a function that we can use to hook into the admin menu. It just calls the previous function and passes a position to it. This way we can set where in the menu our separator should appear. For a list of menu positions, see the menu_position argument in the register_post_type Codex Document.

To add more than one separator, just copy the add_admin_menu_separator(); function as many times as you need, giving them each a new position.

/**
 * Adds Admin Menu Separators
 **/
function admin_menu_separator() {

	// Adds custom separator after comments (at position 30)
	add_admin_menu_separator(30);
}

// Registers admin menu separators
add_action('admin_menu','admin_menu_separator');

The Complete Code

Here are both functions put together. Just place this in your functions.php file in your theme or use in your plugin to easily create a custom separator to organize those custom post types or menu pages.

// Registers admin menu separators
add_action('admin_menu','admin_menu_separator');

/**
 * Create Admin Menu Separator
 **/
function add_admin_menu_separator($position) {

	global $menu;
	$index = 0;

	foreach($menu as $offset => $section) {
		if (substr($section[2],0,9)=='separator')
		    $index++;
		if ($offset>=$position) {
			$menu[$position] = array('','read',"separator{$index}",'','wp-menu-separator');
			break;
	    }
	}

	ksort( $menu );
}

/**
 * Adds Admin Menu Separators
 **/
function admin_menu_separator() {

	// Adds custom separator after comments
	add_admin_menu_separator(30);
}
  • http://andrewledwith.com/ Andrew

    This is exactly what I’ve been looking for. But it seems to not be working for me as intended. I am also using the Jetpack plugin on my site which is placed between Dashboard and a divider per default. When I add your code the Jetpack divider and the new divider get placed where I chose the new one to be. If that’s really confusing I’ve attached a photo. Any thoughts?

    • http://www.xldstudios.com/ Erik Bernskiöld

      I have seen the same problem myself at times with some other plugins too, but I am afraid I don’t quite know the answer as to how to fix it.

  • JD

    Thank you, this was very helpful!