Removing the WordPress Admin Bar – But Leave it for Admins

One of the parts of WordPress I’ve come to rely on is the admin menu. The admin bar is not always suitable for users though. When build ing a web application based on WordPress you most likely don’t want users to see it.

Today we’ll take a look at removing the admin bar, then we’ll take another step and only remove it for certain users.

Removing the Admin Bar

The first we’ll do is look at the code.

As sometimes happens a better way was pointed out.

So take out my call to remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); and use show_admin_bar( false ); instead. Also check out Brady’s comment for some more clarification.

<?php
class WPTT_Screencast{

    function __construct(){
            add_action( 'wp', array( $this, 'no_admin_bar' ) );
    } // __construct

    /**
     * Removes the WordPress admin bar
     *
     * @uses remove_action()    Removes given action
     * @uses add_filter()       Calls 'wp_head' so we can add CSS to remove admin bar margin
     *
     * @since   1.0
     * @author  WP Theme Tutorial, Curtis McHale
     */
    public function no_admin_bar(){

            // removing the bar
            remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 );

            // removing the CSS
            add_filter( 'wp_head', array( $this, 'no_bar_css' ) );

    } // no_admin_bar


    /**
     * Removes the CSS for the admin bar so the site doesn't look screwy.
     *
     * @since   1.0
     * @author  WP Theme Tutorial, Curtis McHale
     */
    public function no_bar_css(){
          echo '<style type="text/css" media="screen">
              html { margin-top: 0px !important; }
              * html body { margin-top: 0px !important; }
              </style>';
    } // no bar CSS

} // WPTT_Screencast

$wptt_screen = new WPTT_Screencast();
?>

The WordPress admin bar is fired on the wp_footer action. So to remove it all we have to do is remove the function attached to the action.

Now even with the bar physically removed from the site, the CSS will still be around. That means your whole site will be pushed down 28px. So now we hook wp_head and print out some CSS to set the top margin to 0.

Let Site Admin’s see it

So we’ve got the bar gone, but like I said I love the WordPress admin bar. Making it stay active for just admins is as simple as wrapping our remove_action and add_filter calls in a check with current_user_can.

<?php
    /**
     * No admin bar unless you're an administrator
     *
     * @uses current_user_can   Checks if current user has provided cap
     * @uses remove_action()    Removes given action
     * @uses add_filter()       Calls 'wp_head' so we can add CSS to remove admin bar margin
     *
     * @since   1.0
     * @author  SFNdesign, Curtis McHale
     */
    public function no_admin_bar(){

        if ( ! current_user_can( 'activate_plugins' ) ){
            remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 );

            // dealing with the CSS
            add_filter( 'wp_head', array( $this, 'no_bar_css' ) );
        } // activate plugins

    } // no_admin_bar
?>

So we’re using current_user_can to check if the current user can activate plugins. For a normal WordPress install this will only be Administrators.

There we go, we have removed the WordPress admin bar.

Curtis McHale

Posts Twitter

Curtis is a web designer/developer specialized in WordPress eCommerce development on WooCommerce. You can get in touch with him about projects on SFNdesign.
My Blog is 4 Times Faster Than Your Blog

3 responses to Removing the WordPress Admin Bar – But Leave it for Admins

  1. Thought I’d follow up with a quick comment just to clarify a couple things. The admin bar is set up on ‘init:10′, so it needs to be disabled before then. Generally, you wrap functionality in a hook so that it can be manipulated elsewhere if need be, but for something simple like this, dropping it directly into functions.php or a plugin should be fine and can still be modified by a plugin executing later in the request lifecycle:

    show_admin_bar( false );

    If you’re concerned about backward compatibility or worry that the function might not exist, then there’s also a filter to toggle the display:

    add_filter( 'show_admin_bar', '__return_false' );

    That’s using a pre-declared function in core that’s convenient for returning false in filters like this. To do the capability check, you can use the same filter with a custom callback like this:

    function themename_show_admin_bar( $show_admin_bar ) {
    return current_user_can( 'activate_plugins' );
    }
    add_filter( 'show_admin_bar', 'themename_show_admin_bar' );

  2. It’s amazing that there is not a simple way to check a user’s role and that we have to revert to using “current_user_can” or some other hackish type deal. Thanks for the function though!

    • Roles aren’t reliable though and a capability could span many roles. On many of the sites I build we end up needing to extend what a handfull (like 5 in 100) of users can do. Adding a single capability to a user is the best way to do that.

      I wish that WP_User_Query could search by capability. Currently you have to get the users by role, then loop through all the users checking for a capability. With that loop you build an array of user id’s that have a capability. Not super efficient, but that’s what we have to deal with.

      Technically `current_user_can` will accept the name of a role as well, it’s just not the right way to do it.

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>