# Sort "My Sites" Alphabetically in the Admin Bar

> Orders the sites in the toolbar's My Sites menu by name instead of by creation date, which makes long lists much easier to scan.

- Type: PHP
- Runs: Run everywhere
- Category: [Multisite](https://wpide.com/library/category/multisite/)
- Tags: admin-bar, my-sites, sorting
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/ms-admin-bar-sort-my-sites/

## Code

```php
if ( ! is_multisite() ) {
    return;
}

add_action( 'admin_bar_menu', function ( $bar ) {
    if ( empty( $bar->user->blogs ) || count( $bar->user->blogs ) < 3 ) {
        return;
    }

    $main  = get_main_site_id();
    $blogs = $bar->user->blogs;

    uasort( $blogs, function ( $a, $b ) use ( $main ) {
        if ( (int) $a->userblog_id === $main ) {
            return -1;
        }
        if ( (int) $b->userblog_id === $main ) {
            return 1;
        }
        return strcasecmp( $a->blogname, $b->blogname );
    } );

    $bar->user->blogs = $blogs;
}, 19 ); // My Sites is built at priority 20.
```

## Notes

The main site stays first; change the comparison to sort by address instead.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Sort "My Sites" Alphabetically in the Admin Bar" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
