How to Create Custom WordPress Admin Toolbar Links
How to Create Custom WordPress Admin Toolbar Links
May 24, 2023
Creating WordPress Admin Toolbar links is a straightforward process using WordPress’s wp_admin_bar
object, which allows you to customize the toolbar in the admin area or even on the frontend when logged in. Here’s how you can do it:
Example Code to Add Links to the Admin Toolbar
You can add the following code to your theme’s functions.php
file or use a plugin like the Code Snippets plugin for a cleaner approach:
Copy to Clipboard
Explanation of the Code
- Hook:
Theadmin_bar_menu
action hook allows you to modify the admin toolbar. - Parent Menu:
Theadd_node
method creates a parent menu item. Use theid
as a unique identifier andtitle
as the menu label. - Child Links:
When adding child links, set theparent
key to theid
of the parent menu. - Standalone Links:
For links that don’t belong to a parent menu, omit theparent
key. - Meta Options:
target
: Use_blank
to open links in a new tab.title
: Sets a tooltip when hovering over the link.class
: Adds a custom CSS class for styling.
Where This Will Appear
- The added links will appear in the WordPress Admin Toolbar, which is visible at the top of the WordPress admin area or the frontend when logged in.
Customization Tips
- You can use conditionals like
is_admin()
orcurrent_user_can()
to control who sees the links. - Add inline or external CSS to style the links further if needed.
Would you like guidance on how to add these links only for specific users or roles?