WordPress 4.3 hide admin bar for non administrators

After updating to WordPress 4.3 users can see the admin bar. I use this code to hide it normally, but this is not working anymore in 4.3.

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
     if (!current_user_can('administrator') && !is_admin()) {
         show_admin_bar(false);
     }
}

Any ideas?

Related posts

5 comments

  1. The function current_user_can refers to capabilities or user role names. So try manage_options instead:

    add_action('after_setup_theme', 'remove_admin_bar');
    
    function remove_admin_bar() {
         // 'manage_options' is a capability assigned only to administrators
         if (!current_user_can('manage_options') && !is_admin()) {
             show_admin_bar(false);
         }
    }
    

    Instead of using the after_setup_theme action you can also add a filter (prefered for newer WP versions):

    add_filter( 'show_admin_bar' , 'handle_admin_bar');
    
    function handle_admin_bar($content) {
         // 'manage_options' is a capability assigned only to administrators
         // here, the check for the admin dashboard is not necessary
         if (!current_user_can('manage_options')) {
             return false;
         }
    }
    
  2. Here is a 6 lines code nugget that will remove the admin bar for non contributor users :

    add_action( 'init', 'fb_remove_admin_bar', 0 );
    function fb_remove_admin_bar() {
        if (!current_user_can('edit_posts')) { // you can change the test here depending on what you want
            add_filter( 'show_admin_bar', '__return_false', PHP_INT_MAX );
        }
    }
    

    Put that in your function.php file or in your custom plugin.

  3. Thanx all for helping. Eventually the problem was a maintenance plugin. When i disabled this it was working again.

  4. Place the code in your theme’s functions.php file

    if (!current_user_can('manage_options')) {
        add_filter('show_admin_bar', '__return_false');
    }
    
  5. Disable the WordPress Admin Bar Using CSS

    You only have to copy and paste the CSS code below in Appearance > Customize > Additional CSS, or your style.css file.

    The CSS code to disable the toolbar is:

    #wpadminbar { display:none !important;}
    

Comments are closed.