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