0

I'm not able to reorder WP Admin menu items when pages added with add_menu_page have complicated slugs. At least I think that's the problem...

First I create the custom menu items, which is working as expected:

function sma_simplify_admin_ui() {
    global $user_ID;
    if ( $user_ID != 1 ) {
        add_menu_page( 'Menu', 'Menu', 'edit_posts', 'nav-menus.php?action=edit&menu=16', '', 'dashicons-menu' );
        add_menu_page( 'Memberships', 'Memberships', 'edit_posts', 'edit.php?post_type=wc_user_membership', '', 'dashicons-groups' );
    }
}
add_action( 'admin_init', 'sma_simplify_admin_ui' );

The new items cannot be reordered. If I put the exact slugs given when they were created, menu_order (and custom_menu_order) simply ignore them and my new items are always at the bottom of the menu.

function sma_reorder_admin_ui($menu_ord) {  
    if (!$menu_ord) return true;
    return array(
        'nav-menus.php?action=edit&menu=16',
        'edit.php?post_type=wc_user_membership',
        /* truncated for clarity, there is more stuff */
    );
}  
add_filter( 'custom_menu_order', 'sma_reorder_admin_ui' ); 
add_filter( 'menu_order', 'sma_reorder_admin_ui' );

Setting a priority when I create them isn't letting me reorder them either, which is really weird...

I'm theorising the slug expected by custom_menu_order & menu_order isn't what I'm giving it, but I don't know what to give it. I've also tried every single slug/key/thing that I get when I vardump $GLOBAL['menu'].

Please help me work out how to reorder my menu items!

2
  • 1
    Inside the sma_reorder_admin_ui function if you just dump or log out the original values of $menu_ord then you should be able to see what it expects. Commented Nov 9, 2021 at 3:12
  • I hadn't thought to vardump $menu_ord specifically, but that does make sense as a way to see the current slugs. Thank you!
    – EdA
    Commented Nov 9, 2021 at 3:18

1 Answer 1

0

I fixed this by changing the action I create the menu items from admin_init to admin_menu. The former runs far too early and I suppose was creating conflicts. I am now able to reorder my items by referencing them by their original slugs.

For future reference and in case it is helpful to others, the WP documentation for add_menu_page specifically recommends hooking to admin_menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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