Here’s an example;
First to figure out the order of the sub menu items based upon its array key you can do a var_dump
on the $submenu global variable which will output the following;
(I’m using the Posts menu and sub menu as an example)
//shortened for brevity....
["edit.php"]=>
array(6) {
[5]=>
array(3) {
[0]=> string(9) "All Posts"
[1]=> string(10) "edit_posts"
[2]=> string(8) "edit.php"
}
[10]=>
array(3) {
[0]=> string(7) "Add New"
[1]=> string(10) "edit_posts"
[2]=> string(12) "post-new.php"
}
[15]=>
array(3) {
[0]=> string(10) "Categories"
[1]=> string(17) "manage_categories"
[2]=> string(31) "edit-tags.php?taxonomy=category"
}
[17]=>
array(3) {
[0]=> string(14) "Sub Menu Title"
[1]=> string(10) "edit_posts"
[2]=> string(17) "sub_menu_page.php"
}
}
We can see that my sub menu item gets added into the array with a key of 17 after the default items.
If for example I want to add my sub menu item, directly after the All Posts sub menu item I need to do so by setting my array key to either 6, 7, 8 or 9 (anything after 5 and before 10 respectively.
This is how you do it…
function change_submenu_order() {
global $menu;
global $submenu;
//set our new key
$new_key['edit.php'][6] = $submenu['edit.php'][17];
//unset the old key
unset($submenu['edit.php'][17]);
//get our new key back into the array
$submenu['edit.php'][6] = $new_key['edit.php'][6];
//sort the array - important! If you don't the key will be appended
//to the end of $submenu['edit.php'] array. We don't want that, we
//our keys to be in descending order
ksort($submenu['edit.php']);
}
Result,
["edit.php"]=>
array(6) {
[5]=>
array(3) {
[0]=> string(9) "All Posts"
[1]=> string(10) "edit_posts"
[2]=> string(8) "edit.php"
}
[6]=>
array(3) {
[0]=> string(14) "Sub Menu Title"
[1]=> string(10) "edit_posts"
[2]=> string(17) "sub_menu_page.php"
}
[10]=>
array(3) {
[0]=> string(7) "Add New"
[1]=> string(10) "edit_posts"
[2]=> string(12) "post-new.php"
}
[15]=>
array(3) {
[0]=> string(10) "Categories"
[1]=> string(17) "manage_categories"
[2]=> string(31) "edit-tags.php?taxonomy=category"
}
}
…give it a try and let us know how you go!
Update 1:
Add this to your functions.php file;
function change_post_menu_label() {
global $menu;
global $submenu;
$my_menu = 'example_page'; //set submenu page via its ID
$location = 1; //set the position (1 = first item etc)
$target_menu = 'edit.php'; //the menu we are adding our item to
/* ----- do not edit below this line ----- */
//check if our desired location is already used by another submenu item
//if TRUE add 1 to our value so menu items don't clash and override each other
$existing_key = array_keys( $submenu[$target_menu] );
if ($existing_key = $location)
$location = $location + 1;
$key = false;
foreach ( $submenu[$target_menu] as $index => $values ){
$key = array_search( $my_menu, $values );
if ( false !== $key ){
$key = $index;
break;
}
}
$new['edit.php'][$location] = $submenu[$target_menu][$key];
unset($submenu[$target_menu][$key]);
$submenu[$target_menu][$location] = $new[$target_menu][$location];
ksort($submenu[$target_menu]);
}
My update includes a slightly easier way to handle the setting of your menu position, you need only stipulate the name of your submenu page and the position you want within the menu. However if you select a submenu page To circumvent that, Kaiser’s example provides some basic checking for that.$location
equal to that of an existing key, it will override that key with yours, thus the menu item will disappear with your menu item in its place. Increment or decrement the number to correctly order your menu if that is the case. Similar, if someone installs a plugin that effects that same menu area, and for which has the same $location
as your submenu item then the same problem will occur.
Update 2:
I’ve added an additional block of code that checks all existing keys in the array against our desired $location
and if a match is found we will increment our $location
value by 1
in order to avoid menu items overriding each other. This is the code responsible for that,
//excerpted snippet only for example purposes (found in original code above)
$existing_key = array_keys( $submenu[$target_menu] );
if ($existing_key = $location)
$location = $location + 1;
Update 3: (script revised to allow sorting of multiple sub menu items)
add_action('admin_init', 'move_theme_options_label', 999);
function move_theme_options_label() {
global $menu;
global $submenu;
$target_menu = array(
'themes.php' => array(
array('id' => 'optionsframework', 'pos' => 2),
array('id' => 'bp-tpack-options', 'pos' => 4),
array('id' => 'multiple_sidebars', 'pos' => 3),
)
);
$key = false;
foreach ( $target_menu as $menus => $atts ){
foreach ($atts as $att){
foreach ($submenu[$menus] as $index => $value){
$current = $index;
if(array_search( $att['id'], $value)){
$key = $current;
}
while (array_key_exists($att['pos'], $submenu[$menus]))
$att['pos'] = $att['pos'] + 1;
if ( false !== $key ){
if (array_key_exists($key, $submenu[$menus])){
$new[$menus][$key] = $submenu[$menus][$key];
unset($submenu[$menus][$key]);
$submenu[$menus][$att['pos']] = $new[$menus][$key];
}
}
}
}
}
ksort($submenu[$menus]);
return $submenu;
}
In the example above you can target multiple sub menus and multiple items per sub menu by setting the parameters accordingly within the $target_menu
variable which holds a multi-dimensional array of values.
$target_menu = array(
//menu to target (e.g. appearance menu)
'themes.php' => array(
//id of menu item you want to target followed by the position you want in sub menu
array('id' => 'optionsframework', 'pos' => 2),
//id of menu item you want to target followed by the position you want in sub menu
array('id' => 'bp-tpack-options', 'pos' => 3),
//id of menu item you want to target followed by the position you want in sub menu
array('id' => 'multiple_sidebars', 'pos' => 4),
)
//etc....
);
This revision will prevent sub menu items over-writing each other if they have the same key (position), as it will cycle through until it finds an available key (position) that does not exist.