Skip to content

Instantly share code, notes, and snippets.

@coulterpeterson
Created February 5, 2024 20:34
Show Gist options
  • Save coulterpeterson/71de69f0fe4e46a6167a9950d5766fa6 to your computer and use it in GitHub Desktop.
Save coulterpeterson/71de69f0fe4e46a6167a9950d5766fa6 to your computer and use it in GitHub Desktop.
WordPress Alphabetical Admin Menu
<?php
// Sort Admin Menu alphabetically
add_filter( 'menu_order', function ($menu_ord) {
global $menu;
$menu_order_to_human_string_array = [];
foreach( $menu_ord as $menu_ord_item ) {
// Remove separators
if( str_contains( $menu_ord_item, 'separator' ) ) {
continue;
}
// Find its counterpart in the global menu
foreach( $menu as $menu_item ) {
if( $menu_ord_item == $menu_item[2] ) {
$menu_order_to_human_string_array[] = [
$menu_ord_item,
$menu_item[0]
];
continue;
}
}
}
// Sort menu based on the human readable strings
uasort($menu_order_to_human_string_array, function($a, $b) {
if ($a[1] > $b[1]) {
return 1;
} elseif ($a[1] < $b[1]) {
return -1;
}
return 0;
});
// Strip off the human readable string before returning
$array_to_return = [];
foreach( $menu_order_to_human_string_array as $menu_item ) {
$array_to_return[] = $menu_item[0];
}
return $array_to_return ;
}, 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment