How can I control the position in the admin menu of items added by plugins?

Having read elsewhere on Stack of two WP plugins forcing identical menu positions (with the likelihood of one then not appearing), I’m wondering how I can control the position of menu items added by plugins.

I already use a function which seems to handle such submenu items in ‘settings’, and another function to reorder default (posts, pages, themes, plugins, settings, etcetera) ‘top level’ items – but which doesn’t change the positioning of such items added by plugins.

function custom_menu_order() {
return array(
//Add items here in desired order.

);
}

add_filter( 'custom_menu_order', '__return_true' );
add_filter( 'menu_order', 'custom_menu_order' );

As an example, of the two top-level menu items added by WooCommerce, one appears above the item added by ContactForm7 and the other below, and it’d be nice to reorder them accordingly – and also, to be able to better reorder items which don’t force a menu position and instead appear at the bottom.

I find it usually works fine for re-ordering default and ‘edit.php?post_type=…’ items, but those with ‘admin.php?page=…’ don’t re-order.

When my re-order function is disabled, the two WooCommerce items (‘edit.php?post_type=product’, and ‘edit.php?post_type=shop_order’) group together as intended, but when the function is reactivated they’re split by ContactForm7 (‘admin.php?page=wpcf7’).

And, one (‘edit.php?post_type=shop_order’) of the WooCommerce CPTs won’t reorder – although the other (‘edit.php?post_type=product’) does.

4

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.

Leave a Comment