Order Admin sub-menu items?

I am adding items to a CPT Admin menu using add_submenu_page which works great, but they are added to the bottom of the sub-menu after the CPT options. I want to be able to have them on top , but I suppose this question can also apply to ordering all Admin based sub-menu items.

What I tried ( not working, I tried several variations),

function custom_menu_order($menu_ord) {
       if (!$menu_ord) return true;

       return array( 

         'edit.php?post_type=page' =>array(
                                      'edit.php?post_type=note',
                                      'edit_pages',
                                      'notes',
                                      )   
                    );
}

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

Would this be because menu_order filter does not take sub-menu’s into account?

3 Answers
3

The filter ‘custom_menu_order’ will not work on the menu order because apply_filters in wp-admin/includes/menu.php supplies false as the filtered content. You can try changing false to $menu and the filter works grand.

Since we obviously can’t touch the core, here’s how I got it to work:

function custom_menu_order(){
    global $submenu;

    $find_page="edit.php";
    $find_sub = 'Post Tags';

    foreach($submenu as $page => $items):
        if($page == $find_page):
            foreach($items as $id => $meta):
                if($meta[0] == $find_sub):
                    $submenu[$find_page][0] = $meta;
                    unset ($submenu[$find_page][$id]);
                    ksort($submenu[$find_page]);
                endif;
            endforeach;
        endif;
    endforeach;
}
add_action('_admin_menu', 'custom_menu_order');

Leave a Comment