0

I want to disable admin bar except administrator.

I tried adding following code snippets into functions.php of my child theme. but none of them works. My WordPress version is 6.4.3. I'd appriciate for any help.

1.

    if ( ! current_user_can( 'manage_options' ) ) {
      add_filter('show_admin_bar', '__return_false');
    }

2. 

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

3. 

    add_filter( 'show_admin_bar' , 'my_function_admin_bar');
    function my_function_admin_bar($show_admin_bar) {
        return ( current_user_can( 'administrator' ) ) ? $show_admin_bar : false;
    }

1 Answer 1

1

Your third one looks like it should work, though it's a bit hard to read. I'd recommend something like this:

add_filter( 'show_admin_bar', 'wpse424492_admin_bar', 10000 );
function wpse424492_admin_bar( $show ) {
    if ( current_user_can( 'manage_options' ) ) {
        // Show the admin bar for Admins.
        return true;
    }
    // Hide the admin bar for anyone else.
    return false;
}

I've added a high priority (10000) to try and have it override any other functions hooked to the filter.

1
  • Hi Pat, Thanks for your help. I've tried your code. But it still doesn't work.
    – Haibo Zhou
    Apr 4 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.