0

I have a requirement and the requirement was, I need to remove some menu options from the front end WordPress admin bar after a user logged in. I have achieved almost all. Now I stuck up with the profile link. I need to display only admin bar and name of the user like Howdy "Ajay" (name of the user) and logout option only. The name of the user should not have any link. But the name of the user is having sitename/wp-admin/profile.php. So how can I remove this href link from the username. Any help would be greatly appreciated.

Below is my code to remove other menu items from admin bar:

function remove_menuitems_from_admin_bar()
{
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo');
    $wp_admin_bar->remove_menu('user-info');
    $wp_admin_bar->remove_menu('site-name');
    $wp_admin_bar->remove_menu('dashboard');
}
add_action('wp_before_admin_bar_render', 'remove_menuitems_from_admin_bar');
2
  • put some code that you tried or put the code that displays admin bar, so i can check and try to help. Oct 24, 2018 at 6:49
  • I have updated my question, please check Oct 24, 2018 at 6:53

1 Answer 1

1

Try the below code,

function remove_menuitems_from_admin_bar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo');
    $wp_admin_bar->remove_menu('user-info');
    $wp_admin_bar->remove_menu('site-name');
    $wp_admin_bar->remove_menu('dashboard');
    $wp_admin_bar->remove_menu('edit-profile', 'user-actions');
    $wp_admin_bar->remove_menu('my-account');

    $user_id = get_current_user_id();
    $current_user = wp_get_current_user();
    if (!$user_id)
        return;

    $avatar = get_avatar($user_id, 26);       
    $howdy = sprintf(__('Howdy, %s'), '<span class="display-name">' . $current_user->display_name . '</span>');
    $class = empty($avatar) ? '' : 'with-avatar';

    $wp_admin_bar->add_menu(array(
        'id' => 'my-account',
        'parent' => 'top-secondary',
        'title' => $howdy . $avatar,
        'meta' => array(
            'class' => $class,
        ),
    ));
}
add_action('wp_before_admin_bar_render', 'remove_menuitems_from_admin_bar');

Hope this helps.

5
  • Hi, still I am getting profile link , no change :( Oct 24, 2018 at 7:33
  • <a class="ab-item" aria-haspopup="true" href="localhost/site_name/wp-admin/profile.php" title="My Account">Howdy, <span class="display-name">Ajay Babu</span></a> if you see in href still I am getting this , copied from inspect element Oct 24, 2018 at 7:36
  • Strange, as i tried this code and works fine. are you sure that you replaced your function with above function in my answer? Oct 24, 2018 at 7:38
  • hey ,some thing got missed from my side. Works perfectly. Thank you. Oct 24, 2018 at 7:42
  • Glad that it helped you. Oct 24, 2018 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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