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');
  • put some code that you tried or put the code that displays admin bar, so i can check and try to help. – Krishna Joshi Oct 24 '18 at 6:49
  • I have updated my question, please check – user3408779 Oct 24 '18 at 6:53
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.

  • Hi, still I am getting profile link , no change :( – user3408779 Oct 24 '18 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 – user3408779 Oct 24 '18 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? – Krishna Joshi Oct 24 '18 at 7:38
  • hey ,some thing got missed from my side. Works perfectly. Thank you. – user3408779 Oct 24 '18 at 7:42
  • Glad that it helped you. – Krishna Joshi Oct 24 '18 at 8:58

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.