Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Last active September 7, 2020 22:05
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save franz-josef-kaiser/4693195 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/4693195 to your computer and use it in GitHub Desktop.
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: 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;
}
<?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', '' );
}
@barrychapman
Copy link

This is good - but it still renders a 'clickable' link to the slug in question defined in the add_submenu_page (11).

<li><a href="11"><div class="separator"></div></a></li>

@franz-josef-kaiser
Copy link
Author

@barrychapman Well, the plugin was written 7 years ago. Nice to read that it still works. Would need an overhaul as it's still using PHP4 syntax. If you got edits, please let me know. I will happily factor them in and file an edit. Thanks.

@barrychapman
Copy link

wow, i didnt realize it had been 7 years.

i feel old

@franz-josef-kaiser
Copy link
Author

Ask me…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment