Move admin menu at the end

The global variable $menu can be manipulated. Here, we are moving the Pages menu item to the end:

moved pages to the end of the menu

Maybe there’s a simpler method to do the recursive array search, I’ve grabbed an example from PHP Manual. The value to be searched has to be inspected inside the $menu var, enable the debug lines to brute force inspect it.

add_action( 'admin_menu', 'move_menu_item_wpse_94808', 999 );

function move_menu_item_wpse_94808() 
{
    global $menu;

    // Inspect the $menu variable:
    // echo '<pre>' . print_r( $menu, true ) . '</pre>';
    // die();

    // Pinpoint menu item
    $move = recursive_array_search_php_91365( 'edit.php?post_type=page', $menu );

    // Validate
    if( !$move )
        return;

    // Store menu item
    $new = $menu[ $move ];

    // Remove menu item, and add previously stored at the end
    unset( $menu[ $move ] );
    $menu[] = $new;
}

// http://www.php.net/manual/en/function.array-search.php#91365
function recursive_array_search_php_91365( $needle, $haystack ) 
{
    foreach( $haystack as $key => $value ) 
    {
        $current_key = $key;
        if( 
            $needle === $value 
            OR ( 
                is_array( $value )
                && recursive_array_search_php_91365( $needle, $value ) !== false 
            )
        ) 
        {
            return $current_key;
        }
    }
    return false;
}

Leave a Comment