badah
2/1/2013 - 6:43 PM

WordPress (mu)plugin to add a separator to the admin menu with a call to the default API. Explanation can be found in the doc block. // Scre

WordPress (mu)plugin to add a separator to the admin menu with a call to the default API. Explanation can be found in the doc block. // Screenshots (main menu separator) http://i.stack.imgur.com/HUmKQ.png // (submenu separator) http://i.stack.imgur.com/6AKDY.png

<?php
defined( 'ABSPATH' ) OR exit;
/** Plugin Name: Example Admin Menu Separator */

add_action( 'admin_menu', 'add_admin_menu_separator' );
function add_admin_menu_separator()
{
	add_menu_page( '', '', 'read', 'wp-menu-separator', '', '', '21' );
	add_submenu_page( 'edit.php?post_type=page', 'wp-menu-separator', '', 'read', '11', '' );
}
<?php
defined( 'ABSPATH' ) OR exit;
/**
 * Plugin Name: Admin Menu Separator
 * Description: Adds a separator on whatver priority is needed.
 */

add_filter( 'parent_file', 'admin_menu_separator' );
function admin_menu_separator( $parent_file )
{
	// Handle main menu separators
	$menu = &$GLOBALS['menu'];
	foreach( $menu as $key => $item )
	{
		if (
			in_array( 'wp-menu-separator', $item )
			AND 5 < count( $item )
			)
		{
			$menu[ $key ][2] = 'separator0';
			$menu[ $key ][4] = 'wp-menu-separator';
			unset(
				 $menu[ $key ][5]
				,$menu[ $key ][6]
			);
		}
	}

	return $parent_file;
}

add_filter( 'parent_file', 'admin_submenu_separator' );
function admin_submenu_separator( $parent_file )
{
	// Handle submenu separators
	$submenu = &$GLOBALS['submenu'];
	foreach( $submenu as $key => $item )
	{
		foreach ( $item as $index => $data )
		{
			// Check if we got the identifier
			if ( in_array( 'wp-menu-separator', $data, true ) )
			{
				// Set the MarkUp, so it gets used instead of the menu title
				$data[0] = '<div class="separator"></div>';
				// Grab our index and temporarily save it, so we can safely override it
				$new_index = $data[2];
				// Set the parent file as new index, so core attaches the "current" class
				$parent_file === $key AND $data[2] = $GLOBALS['parent_file'];
				// Reattach to the global with the new index
				$submenu[ $key ][ $new_index ] = $data;
				// Prevent duplicate
				unset( $submenu[ $key ][ $index ] );
				// Get back into the right order
				ksort( $submenu[ $key ] );
			}
		}
	}

	return $parent_file;
}