1

I would like to disable the admin bar search field when certain users, belonging to specific roles are logged in. I would like to keep the search field available for the role 'admin', but not others.

This post tells me how to remove the admin bar completely, but doesn't give any info regarding how to selectively remove elements of it.

Does anyone have experience with this?

Thanks, jml

5
  • @kaiser - I don't know where to start. Anything helps. I was able to disable the search field for the front-facing site via style.css and header.php in my child theme, but that is only for the outside.
    – jml
    Jan 20, 2014 at 22:04
  • Maybe start by showing us that you're able to run any code in the admin UI or what your search for the admin toolbar and the search field returned.
    – kaiser
    Jan 20, 2014 at 22:08
  • Uhm... I searched this site and the WP support site, to no avail. I haven't found any information on doing this. It could be that it's a yes or no question to "is it possible", but I have no experience with it. Here's the only stackexchange query that is even remotely related: wordpress.stackexchange.com/questions/54235/… ...Also, not sure what you mean by "show us that you're able to run any code in the admin UI" - that seems to be a bit off topic.
    – jml
    Jan 21, 2014 at 0:55
  • Please file edits for additional info (instead of comments). For your reasearch: Try "adminbar".
    – kaiser
    Jan 21, 2014 at 1:25
  • I updated my post.
    – jml
    Jan 21, 2014 at 2:05

1 Answer 1

5

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

2
  • This answer not address the question which is how to remove the search for specific users/roles.
    – s_ha_dum
    Nov 23, 2015 at 3:06
  • ok, I added that...
    – C C
    Nov 23, 2015 at 3:24

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.