public
Last active

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

  • Download Gist
admin_menu_separator.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?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;
}
example_separator.php
PHP
1 2 3 4 5 6 7 8 9 10
<?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', '' );
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.