Just in case anyone trips over this…here’s how you remove “search” and some examples of other things you can remove from the admin bar:
public function remove_admin_menu_bar_items ($wp_toolbar) {
$wp_toolbar->remove_node( 'my-sites' );
$wp_toolbar->remove_node( 'wp-logo' );
$wp_toolbar->remove_node( 'new-content' );
$wp_toolbar->remove_node( 'view' );
$wp_toolbar->remove_node( 'logout' ); //remove "logout" under "howdy"
$wp_toolbar->remove_node( 'user-info' ); //remove "youraccount" under "howdy"
$wp_toolbar->remove_node( 'edit-profile' ); //remove "edit my profile" under "howdy"
$wp_toolbar->remove_node( 'search' ); // remove the search element
return $wp_toolbar;
}
add_filter( 'admin_bar_menu', 'remove_admin_menu_bar_items' );
Codex reference: https://codex.wordpress.org/Function_Reference/remove_node
And to completely answer the question, you can execute this code (using conditional logic), based on first querying the current user if you want to test for a specific role/capability:
$current_user = wp_get_current_user();
$current_user will be an object and one of the properties is the role(s) the user has.
Codex reference: https://codex.wordpress.org/Function_Reference/wp_get_current_user