Discover in this tutorial how to insert a custom menu in WordPress admin bar using code.
In previous tutorials I explained you how to customize the WordPress admin bar by adding a custom link, showing the active theme or indicate if indexing is active.
But if you want to add several links that you want to have always at hand is better to group them into a menu, so you do not clutter the admin bar with many items.
In this tutorial you are going to learn how to add a custom menu to the admin bar. This would be the result:
Here we go:
Snippet to add a custom menu to the WordPress admin bar
Add the following code snippet to your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this code use the admin_bar_menu
hook to add a menu called “My menu” that on mouse over will show 3 sub-items:
- Entries: direct link to the list of entries.
- Plugins: direct link to the list of plugins.
- Permalinks: direct link to the permalinks settings.
This is an example that I use on several websites, as they are sections that I find handy to have at hand. But the fun thing is that you can modify the snippet to your liking. You only have to customize the following arguments of the array:
id
: identifier of the menu item (required).parent
: indicates the parent item of the menu (only in sub-items).title
: the text to be displayed in the administration bar.href
: the link where it will go when clicked.meta
: allows you to customize some extra parameters (optional).title
: the text that will be displayed when hovering.target
: determines if the link opens in the same page or a new one (if not defined it opens in the same page).class
: CSS class to be added to the element.
Now you can add that link you consider essential to have it always at hand. 😉
Tip: to modify the href
argument just visit the URL you want to be the destination and use the part after /wp-admin/. For example, the permalink URL is https://yourwebsite.com/wp-admin/options-permalink.php and that’s why you add admin_url( 'options-permalink.php').
You can also play with the priority with which the hook is executed, so that it is displayed more to the left or to the right on the bar. In this case we use 50 so that it is quite far to the left. If you want it to be displayed further to the right you can increase the number (100, 200…).
Conclusions
Do you want to add a custom menu to the WordPress admin bar? Now you know how.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂