Here’s a quick and dirty way to get what you want.
Background
WordPress stores admin menu sections in a global array called $menu. To add a separator you add an element to the $menu array using an index that is between the indexes of the options that you want to separate.
Using the add_admin_menu_separator() function
So I’ve written a function to encapsulate the logic for this I called add_admin_menu_separator(). You’ll need to pick an array index number that is higher than the option after which you want to add a separator, and then call the function add_admin_menu_separator() passing said index as your parameter.
For example:
add_admin_menu_separator(37);
The add_admin_menu_separator() function itself
Here’s the definition of the function add_admin_menu_separator() which you can copy into your theme’s functions.php file. Yes it is arcane but then so is the code that creates and uses the global $menu array. (Plans are to eventually deprecate it, thankfully, but that’ll probably be a few years.)
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 );
}
Finding the index into $menu that you need
To figure out what index number you need you can do a var_dump() of $GLOBALS['menu'] from within an admin_init hook. Here’s a bit of code you can drop into your theme’s functions.php file temporarily to see what the values are. This will only work when requesting a URL starting with /wp-admin/(but be sure to do with with FTP and not the built in theme editor or you’ll loose access to your site, at least until you get FTP access to your theme’s functions.php file!):
add_action('admin_init','dump_admin_menu');
function dump_admin_menu() {
if (is_admin()) {
header('Content-Type:text/plain');
var_dump($GLOBALS['menu']);
exit;
}
}
See Also:
BTW, you might find these links useful in general for working with admin menus:
Had some issues with separators not being added, it was due to the way wordpress removes duplicate separators. Adding ksort($menu); to the bottom of the function corrected the issue.
In short, you’re looking for usage of the add_menu_page() function, which takes a number of arguments and displays an additional item in the administrative menu as a sibling of “Dashboard”, “Posts”, “Media”, etc.
The below ads an action to WordPress that is run when the admin menu is rendered. The admin menu is an array and all you’re doing, here, is adding an array element that defines a separator, in the position you specify.
Note that, though this rather resembles the currently accepted answer, the currently accepted answer did not work for me (and, judging from the comments, for several others).
Here’s a quick and dirty way to get what you want.
Background
WordPress stores admin menu sections in a global array called
$menu
. To add a separator you add an element to the$menu
array using an index that is between the indexes of the options that you want to separate.Using the
add_admin_menu_separator()
functionSo I’ve written a function to encapsulate the logic for this I called
add_admin_menu_separator()
. You’ll need to pick an array index number that is higher than the option after which you want to add a separator, and then call the functionadd_admin_menu_separator()
passing said index as your parameter.For example:
The
add_admin_menu_separator()
function itselfHere’s the definition of the function
add_admin_menu_separator()
which you can copy into your theme’sfunctions.php
file. Yes it is arcane but then so is the code that creates and uses the global$menu
array. (Plans are to eventually deprecate it, thankfully, but that’ll probably be a few years.)Finding the index into
$menu
that you needTo figure out what index number you need you can do a
var_dump()
of$GLOBALS['menu']
from within anadmin_init
hook. Here’s a bit of code you can drop into your theme’sfunctions.php
file temporarily to see what the values are. This will only work when requesting a URL starting with/wp-admin/
(but be sure to do with with FTP and not the built in theme editor or you’ll loose access to your site, at least until you get FTP access to your theme’sfunctions.php
file!):See Also:
BTW, you might find these links useful in general for working with admin menus:
wp-admin-menu-classes.php
Although my admin menu classes don’t currently offer an easy way to add separators I think I’ll now add that when I have time.
It’s easy as:
Note: The default separator position (
$menu[4]
) can be different depending of which configuration you (or your plugins) have done before this hook.Of course, you can also tweak the
menu-top-first
andmenu-top-last
classes introspection.Had some issues with separators not being added, it was due to the way wordpress removes duplicate separators. Adding
ksort($menu);
to the bottom of the function corrected the issue.This builds on MastaBaba’s answer, but does not replace anything at the position you specify – it just adds the separator afterwards.
Also uses an anonymous function so it’s only compatible with PHP version 5.3 and above.
You can simply add this in functions.php
Where 49 is separator position, You have to remember that separator position can replace your menu element if it has the same position.
It’s a little hard to determine what you’re trying to do, but I think you’re looking to add a top-level entry in the WordPress admin menu.
See this documentation in the WordPress Codex.
In short, you’re looking for usage of the
add_menu_page()
function, which takes a number of arguments and displays an additional item in the administrative menu as a sibling of “Dashboard”, “Posts”, “Media”, etc.Hope I got it right. 🙂
The below ads an action to WordPress that is run when the admin menu is rendered. The admin menu is an array and all you’re doing, here, is adding an array element that defines a separator, in the position you specify.
From here:
Note that, though this rather resembles the currently accepted answer, the currently accepted answer did not work for me (and, judging from the comments, for several others).