Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Created May 2, 2023 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathaningram/f30551c159b1806ac583f1ec110f1b65 to your computer and use it in GitHub Desktop.
Save nathaningram/f30551c159b1806ac583f1ec110f1b65 to your computer and use it in GitHub Desktop.
Sort WP Admin Settings Menu Alphabetically
// Function to sort settings menu items alphabetically
function ni_sort_settings_menu_items_alphabetically() {
global $submenu;
// Check if the settings menu exists
if (isset($submenu['options-general.php']) && is_array($submenu['options-general.php'])) {
// Sort the items under the settings menu alphabetically
usort($submenu['options-general.php'], function ($a, $b) {
// Ensure both array elements have the 0-index set and are strings
if (isset($a[0]) && isset($b[0]) && is_string($a[0]) && is_string($b[0])) {
return strcasecmp($a[0], $b[0]);
}
return 0;
});
}
}
add_action('admin_menu', 'ni_sort_settings_menu_items_alphabetically', 999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment