Removing WP Admin Bar items based on User
Here’s a little snippet of code I hope you will find useful. We’re working on a custom feature that basically allows a specific user to be able to edit their own page. This would allow a client to designate a specific user the ability to edit one page, just by assigning them as author. In a more global aspect, we’d want to get rid of the dropdowns in the WordPress Admin Bar for that user. This is how you would go about it:
[code xclass=”hidden”]
// remove links/menus from the admin bar
function remove_wpmenu_content($wp_admin_bar) {
$user_ID = get_current_user_id();
if ($user_id = 2) { //set the ID of the user here
$wp_admin_bar->remove_node(‘new-content’);
$wp_admin_bar->remove_node(‘comments’);
$wp_admin_bar->remove_node(‘environment’);
$wp_admin_bar->remove_node(‘wpseo-menu’);
}
}
add_action(‘admin_bar_menu’, ‘remove_wpmenu_content’, 999);
[/code]
You can also do this by user role.
[code xclass=”prettyprint”]
// remove links/menus from the admin bar
function remove_wpmenu_content($wp_admin_bar) {
if (current_user(‘author’)) {
$wp_admin_bar->remove_node(‘new-content’);
$wp_admin_bar->remove_node(‘comments’);
$wp_admin_bar->remove_node(‘environment’);
$wp_admin_bar->remove_node(‘wpseo-menu’);
}
}
add_action(‘admin_bar_menu’, ‘remove_wpmenu_content’, 999);
[/code]
Hope this helps you!