Wordpress - How can I control the position in the admin menu of items added by plugins?
The existing answers are fine, but if you would add a new custom post type, you would have to re-edit those functions again and again.
To fix this, I developed this small function. Just define your $new_positions
inside the my_new_menu_order
function:
/**
* Activates the 'menu_order' filter and then hooks into 'menu_order'
*/
add_filter('custom_menu_order', function() { return true; });
add_filter('menu_order', 'my_new_admin_menu_order');
/**
* Filters WordPress' default menu order
*/
function my_new_admin_menu_order( $menu_order ) {
// define your new desired menu positions here
// for example, move 'upload.php' to position #9 and built-in pages to position #1
$new_positions = array(
'upload.php' => 9,
'edit.php?post_type=page' => 1
);
// helper function to move an element inside an array
function move_element(&$array, $a, $b) {
$out = array_splice($array, $a, 1);
array_splice($array, $b, 0, $out);
}
// traverse through the new positions and move
// the items if found in the original menu_positions
foreach( $new_positions as $value => $new_index ) {
if( $current_index = array_search( $value, $menu_order ) ) {
move_element($menu_order, $current_index, $new_index);
}
}
return $menu_order;
};
when you're creating a post type with register_post_type() you can set the menu position:
menu_position (integer) (optional) The position in the menu order the post type should appear. show_in_menu must be true.
Default: null - defaults to below Comments 5 - below Posts 10 - below Media 15 - below Links 20 - below Pages 25 - below comments 60 - below first separator 65 - below Plugins 70 - below Users 75 - below Tools 80 - below Settings 100 - below second separator
If items have the same menu position they are sorted alphabetically.
in your own plugin you can set the level. if you're trying to change the menu position of a plugin you haven't created, many of them may have it pluggable, or you can to edit their calls.
To change top level admin menu items order you'll need two hooks
, two filters
, and one function
. Put the following code in your current theme's functions.php
:
function wpse_custom_menu_order( $menu_ord ) {
if ( !$menu_ord ) return true;
return array(
'index.php', // Dashboard
'separator1', // First separator
'edit.php', // Posts
'upload.php', // Media
'link-manager.php', // Links
'edit-comments.php', // Comments
'edit.php?post_type=page', // Pages
'separator2', // Second separator
'themes.php', // Appearance
'plugins.php', // Plugins
'users.php', // Users
'tools.php', // Tools
'options-general.php', // Settings
'separator-last', // Last separator
);
}
add_filter( 'custom_menu_order', 'wpse_custom_menu_order', 10, 1 );
add_filter( 'menu_order', 'wpse_custom_menu_order', 10, 1 );
The returned array of top level admin menu items, above, represents menu items inserted by core, in their default order. To include menu items added by plugins, we have to add them to this array. Let's say we have two plugins added and activated ( for example: Wordfence
and NextCellent Gallery
). We have to find names of these menu items, first. When we click on Wordfence
's top level menu item, the resulting URL will end with ?page=Wordfence
. The part after ?page=
is our name ( Wordfence
). For NextCellent Gallery
, the name will be nextcellent-gallery-nextgen-legacy
. Now, let's add these items to our array:
return array(
'index.php', // Dashboard
'separator1', // First separator
'edit.php', // Posts
'upload.php', // Media
'link-manager.php', // Links
'edit-comments.php', // Comments
'edit.php?post_type=page', // Pages
'separator2', // Second separator
'themes.php', // Appearance
'plugins.php', // Plugins
'users.php', // Users
'tools.php', // Tools
'separator3', // Third separator
'options-general.php', // Settings
'separator-last', // Last separator
'Wordfence', // Wordfence
'nextcellent-gallery-nextgen-legacy', // NextCellent Gallery
);
We can, now, move items of this array, up and down, to get the final order.
Note: you can use Admin Menu Editor plugin, for easy drag and drop actions, as well.