2

I need to add a link to the drop-down user menu in the admin bar. Is there a hook or function for this?

WP admin bar user drop-down menu

2

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:support@domain.com',
        '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.

| improve this answer | |
0

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. :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.