disable admin-bar search field for specific roles

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.

Read More

Does anyone have experience with this?

Thanks,
jml

Related posts

1 comment

  1. 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:

    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

Comments are closed.