Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to Hide Buddypress Admin Bar For Everyone Except Admin WordPress

  • SOLVED

I have a site at [[LINK href="http://pefriends.com"]]pefriends.com[[/LINK]], and I would only like to display the black Buddypress admin bar at the top for admin, and no one else.

How would you do this?

Here is the code within bp-core.php...

/* If BP_DISABLE_ADMIN_BAR is defined, do not load the global admin bar. */
if ( !defined( 'BP_DISABLE_ADMIN_BAR' ) )
require ( BP_PLUGIN_DIR . '/bp-core/bp-core-adminbar.php' );

Answers (7)

2011-07-15

John Cotton answers:

In your functions.php


if( !current_user_can('administrator') ) {
define( “BP_DISABLE_ADMIN_BAR’, true );
}


Spencer Barfuss comments:

That didn't work in the functions.php file.


John Cotton comments:

Try it in bp-custom.php in the BuddyPress plugin folder.


Spencer Barfuss comments:

I'm looking all throughout the plugin folder, and can't seem to find the bp-custom.php file. Do you know where it is?


John Cotton comments:

You need to create it.

See [[LINK href="http://codex.buddypress.org/extending-buddypress/bp-custom-php/"]]here[[/LINK]]


Spencer Barfuss comments:

Pasting this produces this code - [[LINK href="http://cl.ly/2x2M2w26113m0Q0f3B2G"]]http://cl.ly/2x2M2w26113m0Q0f3B2G[[/LINK]]


John Cotton comments:

OK, so it was a typo in my first instruction.

This definitely works, I've just tried it!

In your FUNCTIONS.php file put this


if( !current_user_can('administrator') ) {
define( BP_DISABLE_ADMIN_BAR, true );
}


Spencer Barfuss comments:

Brilliant! It works! Thank you so much! If I add another $5, could you help me configure the code to where I can specify certain usernames where the admin bar will show, and then for everyone else, it won't show?


John Cotton comments:

I'd discourage you from doing that as it's a maintenance headache - you should really use roles.

But if you must, this will work - just change/add to the login names array.

It means that only admins or those with a login in the list will see the bar.


global $current_user;

$logins = array( 'user_login1', 'user_login2', 'user_login3' );

if( !current_user_can('administrator') || !in_array( $current_user->user_login, $logins ) ) {
define( BP_DISABLE_ADMIN_BAR, true );
}


Spencer Barfuss comments:

I am using user roles. I will have some people as Contributors. So, I am assuming that with the code you provided, they will be able to see the Buddypress Admin bar as well? Because that is what I would like, for Contributors to be able to see the bar as well.

Thanks for your help!


John Cotton comments:

If they're either contributors or admins then



if( !current_user_can('administrator') && !current_user_can('contributor') ) {

define( BP_DISABLE_ADMIN_BAR, true );

}

2011-07-15

Rashad Aliyev answers:


define('BP_DISABLE_ADMIN_BAR', true); put this code to your bp-custom.php.


[[LINK href="http://codex.buddypress.org/theme-development/modifying-the-buddypress-admin-bar/hiding-the-buddypress-admin-bar/"]]Or Look at here[[/LINK]]


Rashad Aliyev comments:

More details [[LINK href="http://codex.buddypress.org/theme-development/modifying-the-buddypress-admin-bar/"]]here[[/LINK]]..


Spencer Barfuss comments:

I don't want to turn it off completely though, only for anyone who is not an admin.

2011-07-15

Eric Looi answers:

Alternatively, you can use just this plugin: [[LINK href="http://wordpress.org/extend/plugins/buddypress-admin-bar-mods/"]]http://wordpress.org/extend/plugins/buddypress-admin-bar-mods/[[/LINK]] or this: [[LINK href="http://wordpress.org/extend/plugins/remove-buddypress-admin-bar/"]]http://wordpress.org/extend/plugins/remove-buddypress-admin-bar/[[/LINK]]

2011-07-15

Navjot Singh answers:

Try this

if (!is_admin() ) define( 'BP_DISABLE_ADMIN_BAR', true );

code in bp-custom.php


Spencer Barfuss comments:

Ok, I added your code to it, but I guess I need to specify a specific username, instead of simply 'admin'. Does that make sense?

So, if the admin's username was <strong>freddy</strong>, I want to block the admin bar for everyone except <strong>freddy</strong>.

It hid the admin bar, but I'm an admin and need it to show for me.


Navjot Singh comments:

You mean the code has hid the admin bar for admin account as well? is_admin() is a function to check whether the current logged in user is an administrator or not. You don't need to edit it.


Spencer Barfuss comments:

Yes, it's hidden from admin as well.

2011-07-15

Romel Apuya answers:

try..


if($current_user->user_login != 'admin'){
define( “BP_DISABLE_ADMIN_BAR’, true );
}

2011-07-15

Ram Kumar answers:

You could use a function inside your theme's functions file to selectively disable it for specific users.
function disable_bar_for_user( $ids ) {

if( !is_user_logged_in() )
return;

global $current_user;

if( is_numeric( $ids ) )
$ids = (array) $ids;

if( !in_array( $current_user->data->ID, $ids ) )
return;

add_filter( 'show_admin_bar', '__return_false', 9 );
}


Then call it for the user or users you want to disable the bar for..
<strong>Single User:</strong>
disable_bar_for_user(1);

<strong>Multiple users:</strong>
disable_bar_for_user(array(1,2,3));

If you want to just turn it off altogether, then the following should do it(instead of the function).

add_filter( 'show_admin_bar', '__return_false', 9 );
Hope that helps.. :)


Ram Kumar comments:

You could use a function inside your theme's functions file to selectively disable it for specific users.
function disable_bar_for_user( $ids ) {

if( !is_user_logged_in() )
return;

global $current_user;

if( is_numeric( $ids ) )
$ids = (array) $ids;

if( !in_array( $current_user->data->ID, $ids ) )
return;

add_filter( 'show_admin_bar', '__return_false', 9 );
}


Then call it for the user or users you want to disable the bar for..
<strong>Single User:</strong>
disable_bar_for_user(1);

<strong>Multiple users:</strong>
disable_bar_for_user(array(1,2,3));

If you want to just turn it off altogether, then the following should do it(instead of the function).

add_filter( 'show_admin_bar', '__return_false', 9 );
Hope that helps.. :)


Spencer Barfuss comments:

Your code would be ideal, but I need to make it work with the pasted code above from the bp-core.php file. I added it to my original question. Check it above, and then if you can, work your code into that one. I absolutely stink at PHP.

Thanks!

2011-07-15

Svilen Popov answers:

if (is_user_logged_in() && current_user_can('administrator') && !defined('BP_DISABLE_ADMIN_BAR'))
require ( BP_PLUGIN_DIR . '/bp-core/bp-core-adminbar.php' );


Spencer Barfuss comments:

That code produced this error - http://cl.ly/111b0S2P0T0B0H2k413u


Svilen Popov comments:

Try to add in functions.php a global variable:

global $tmp_admin;
$tmp_admin = $current_user_can('administrator');


and then in bp-core.php:

global $tmp_admin;
if ($tmp_admin && !defined( 'BP_DISABLE_ADMIN_BAR' ) )
require ( BP_PLUGIN_DIR . '/bp-core/bp-core-adminbar.php' );


Spencer Barfuss comments:

Pasted as you have it here, it gives me this error - [[LINK href="http://cl.ly/3R472y1m023l3l1X1m2L"]]http://cl.ly/3R472y1m023l3l1X1m2L[[/LINK]].


Svilen Popov comments:

remove <strong>$</strong> from <strong>$current_user_can('administrator');</strong>

<blockquote>global $tmp_admin;
$tmp_admin = <strong>current_user_can('administrator');</strong></blockquote>