最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

admin bar - How to remove logged user profile href link

matteradmin11PV0评论

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');

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');
Share Improve this question edited Oct 24, 2018 at 8:20 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Oct 24, 2018 at 6:44 user3408779user3408779 1035 bronze badges 2
  • put some code that you tried or put the code that displays admin bar, so i can check and try to help. – Krishna Joshi Commented Oct 24, 2018 at 6:49
  • I have updated my question, please check – user3408779 Commented Oct 24, 2018 at 6:53
Add a comment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far