I need to add a link to the drop-down user menu in the admin bar. Is there a hook or function for this?
2 Answers
You want to use $wp_admin_bar->add_node( $args ).
Below is a tested working example.
function wpse_add_toolbar_edit($wp_admin_bar) {
$wp_admin_bar->add_node( array(
'id' => 'mylink',
'title' => 'My New Link',
'href' => 'mailto:[email protected]',
'parent' => 'user-actions'
) );
}
add_action('admin_bar_menu', 'wpse_add_toolbar_edit', 999);
Note: The parent
param adds the link to an existing ID. If you need to find the correct ID to add your new link to you can var_dump($wp_admin_bar);
and look through the output for the correct ID.
-
1Is there a way to position my link between "Edit My Profile" and the "Log Out" link?– user60726Jun 20, 2019 at 16:36
-
That is more complicated. Have a look at wordpress.stackexchange.com/questions/125997/… maybe the last answer will help. Jun 20, 2019 at 16:43
Follow the steps to add link between logout and edit my profile.
1. remove the default logout node
$wp_admin_bar->remove_node( 'logout' );
2.add the link that you want to put between logout and edit my profile.
$wp_admin_bar->add_node([
'id' => 'link-id',
'title' => 'Link Title',
'href' => get_site_url(null, 'site-path'),
'parent' => 'user-actions'
]);
3. Add the logout node back to the list using:
$wp_admin_bar->add_node([
'id' => 'logout',
'title' => 'Log Out',
'href' => wp_logout_url(),
'parent' => 'user-actions'
]);
Just manipulated the above function in order to insert the link between logout and profile.
Final Code:
add_action( 'admin_bar_menu', 'adjust_admin_menu_bar_items' , 999);
function adjust_admin_menu_bar_items ($wp_admin_bar) {
$user = wp_get_current_user();
$wp_admin_bar->add_node([
'id' => 'link-id',
'title' => 'Link Title',
'href' => get_site_url(null, 'site-path'),
'parent' => 'user-actions'
]);
$wp_admin_bar->add_node([
'id' => 'logout',
'title' => 'Log Out',
'href' => wp_logout_url(),
'parent' => 'user-actions'
]);
}
Hope this will help. :)