How do I remove the entire left nav bar in admin for certain level users?

I know how to remove items from the left nav bar (hook into admin_menu and do global $menu; unset( $menu[ __( "Posts" ) ] ); for example). But it still shows a left nav bar (with nothing on it). I want the entire left nav bar gone.

I have tried this and it doesn’t work:

//This doesn't work (in any hook, or plugin constructor)
show_admin_bar(false);

//This also doesn't work
add_filter('show_admin_bar', '__return_false');

What is the hook/function to do this?

2 Answers
2

You could creatively use output caching to capture the adminbar and then discard it like so:

<?php
// this filter runs in menu-header.php L37 right before the admin menu is rendered
add_filter( 'parent_file', function( $parent_file ){
    ob_start();
    return $parent_file;
} );
// runs after the output
add_action( 'in_admin_header', function(){
    ob_clean(); // discard output
    echo '<div id="wpcontent">'; 
} );

Please do not actually consider using this in any production environment. It does not provide any security and it might break various things (for example the admin toolbar). I consider this an educational answer to the problem “how to use PHP output caching and WP filters to move remove stuff?”. You are probably better off just using CSS.

Leave a Comment