Posted on January 22, 2013 by Curtis McHale

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.