7

When interacting with the admin menu, for instance via add_menu_page, there is a $position parameter. The inline docs indicate that this should be an integer. The Codex agrees, but adds a note that you can use a float if you make it a string (e.g. '63.3'). In wp-admin/includes/menu.php the array is sorted by comparing the keys with strnatcasecmp.

With this in mind, one could set a menu position of '63-foo' or '63-bar' to avoid position conflicts. This would be especially helpful for plugins, where they currently may set a position which can cause conflicts in some themes or with other plugins.

Using strings appears to work just fine. I combed the source and didn't find any issues, but maybe I've missed something. Are there any reasons why a string can't or shouldn't be used for the menu position?

3
  • Is 63-foo really any better than 63? The lower alpha plugin name would (if that were the string) would just load first which is really no different than whatever plugin is loaded last trumping the 63. Dec 10 '13 at 20:27
  • 1
    The issue is that if two menu items are given a position of 63, the latter overrides the former (silently, causing a head-scratcher). If one is '63-foo' and another is '63-bar', they'll both show. Dec 10 '13 at 21:00
  • The position is only a Boolean value and if you have two or more plugins with the same position value, then can you not control this order. Is the job of WordPress. Currently we developer haven't possibility to control of order in each install of our plugins.
    – bueltge
    Jan 2 '14 at 9:29
1

Other than sorting with strnatcasecmp(), the only other place I see where the key might be used is if someone is applying custom menu ordering. However, since the menu is already being sorted based on the key order, it is unlikely that the keys would play any role in custom sorting. I can see no other reason why it would cause a problem, so it is probably safe to do. I would open up a ticket to get the inline docs changed though.

1

This is part of a script I'm using for my theme. It looks for an empty position that's close to your desired position. Seems a simple enough solution to me?

//work out "safe" menu position
    global $menu;
    $safe_menu_position = 30;
    //while there IS a value
    while( !empty( $menu[$safe_menu_position] ) )
    {
        --$safe_menu_position;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.