How to Reorder Admin Toolbar Quicklinks in Alphabetical Order
-
Good Day,
I would like to reorder your plugin’s admin toolbar quicklinks in alphabetical order (or custom order).
Can you guys provide a code snippet that will allow me to do that?
Note: It would be nice to incorporate the change into your plugin as well (with the exception of always displaying the “Dashboard” link at the top of the reordered list).
Thank you!
-
Hello @generosus,
Yeah, sure! I have forwarded your request to the dev team, and as soon as I receive the solution, I will share it with you.
About incorporating this change into the plugin, we’ll discuss this in internal meeting. Thanks! ??
Hi,
In the
class-wpa-adminbar.php
file, here’s the code for Admin Bar menu options on lines # 39– 40, where these two menu options are added: “Dashboard” and “Refresh Stats.” The other menu options are shown through line#42 when the pro plugin and add-ons (Woo, EDD, Campaigns) are activated.Now the problem here is that if we make a filter for you, this could conflict with the current flow as you can see the filters are already applied according to the activated plugin. (line#42)
So, we can suggest you comment the line#42 and add other menu options manually after line#40 in the sequence you want. Following are the
$menus['xxx']
supported:$menus['analytify-dashboard&show=detail-realtime'] = ( 'Real Time', 'wp-analytify-pro' ) ;
$menus['analytify-dashboard&show=search-terms'] = ( 'Search Terms', 'wp-analytify-pro' );
$menus['analytify-dashboard&show=detail-demographic'] = __( 'Demographics', 'wp-analytify-pro' );
$menus['analytify-woocommerce'] = __('WooCommerce', 'wp-analytify-woocommerce');
$menus['page=edd-dashboard'] = __( 'EDD', 'wp-analytify-edd' );
$menus['analytify-campaigns'] = __( 'Campaigns', 'wp-analytify-campaings' );Let me know if this fulfills your current requirements.
Hi @emma24,
Thank you so much! That worked great, except as follows:
- I commented out Lines 42 and 44-46.
- I corrected two of the $menus line entries you provided. They have a missing underscore (“__”) before the parenthesis. They should be:
$menus['analytify-dashboard&show=detail-realtime'] = __( 'Real Time', 'wp-analytify-pro' );
$menus['analytify-dashboard&show=search-terms'] = __( 'Search Terms', 'wp-analytify-pro' );Result: Quicklinks in Alphabetical Order (Except “Dashboard”)
Caveat: At your next plugin revision, the above is going to be wiped out, so in reality, a php code snippet would be preferred. Perhaps the Hint provided below can help you develop one.
Hint: The php code snippet can be developed to reorder the sub-menu links (i.e., quicklinks) based on their identifiers (id) as noted here. Along these lines, I used the code snippet provided below to reorder my WP Admin Toolbar menu links in alphabetical order, but couldn’t get it to work for sub-menu links (i.e., quicklinks). Perhaps your PHP gurus can tweak it (using WP functions) to make it work for this topic.
Again, thank you!
—————-
Code Snippet to Reorder WP Admin Toolbar Menu Links in Alphabetical Order:
Reference: How Can I Reorder Admin Bar Items
// Reorder Admin Toolbar Menu Items
function reorder_admin_bar() {
global $wp_admin_bar;
// The desired order of identifiers (items) (Note: List the Item ID minus the 'wp-admin-bar" prefix)
$IDs_sequence = array(
'wp-logo',
'site-name',
'updates',
'new-content',
'comments',
'edit',
'analytify',
'avada',
'tribe-events',
'wpfc-toolbar-parent',
'gform-forms',
'gappointments',
'perfmatters',
'rank-math'
);
// Get an array of all the toolbar items on the current page
$nodes = $wp_admin_bar->get_nodes();
// Perform recognized identifiers
foreach ( $IDs_sequence as $id ) {
if ( ! isset($nodes[$id]) ) continue;
// This will cause the identifier to act as the last menu item
$wp_admin_bar->remove_menu($id);
$wp_admin_bar->add_node($nodes[$id]);
// Remove the identifier from the list of nodes
unset($nodes[$id]);
}
// Unknown identifiers will be moved to appear after known identifiers
foreach ( $nodes as $id => &$obj ) {
// There is no need to organize unknown children identifiers (sub items)
if ( ! empty($obj->parent) ) continue;
// This will cause the identifier to act as the last menu item
$wp_admin_bar->remove_menu($id);
$wp_admin_bar->add_node($obj);
}
}
add_action( 'wp_before_admin_bar_render', 'reorder_admin_bar', 999 );One other thing … I know my request goes beyond the scope of this support forum, so feel free to develop the code snippet only if you have time and enjoy the challenge ??
Otherwise, this topic is “Resolved.”
Thank you!
Interesting! Thanks @generosus for your contribution.
I’m adding this to our roadmap so we can consider this later on. Thank you! ??
Solution:
Use the code snippet provided below.
If you don’t mind, please add it to your documentation.
Thank you!
————————–
add_filter( 'wp_before_admin_bar_render', function( $items ) {
global $wp_admin_bar;
// Get all current menu items
$admin_bar_items = $wp_admin_bar->get_nodes();
// Filter the items that start with 'analytify'
$analytify_items = array_filter($admin_bar_items, function($item) {
return strpos($item->id, 'analytify') === 0;
});
// Sort the filtered items alphabetically by title
usort($analytify_items, function($a, $b) {
return strcmp($a->title, $b->title);
});
// Remove the original analytify items from the admin bar
foreach ($analytify_items as $item) {
$wp_admin_bar->remove_menu($item->id);
}
// Re-add the anlaytify items in alphabetical order
foreach ($analytify_items as $item) {
$wp_admin_bar->add_menu((array) $item);
}
} );Sure! Thanks for the code snippet. We’ll add this to our documentation for Analytify Filters.
- You must be logged in to reply to this topic.