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

Customize 3.3 toolbar - frontend WordPress

  • SOLVED

I have a WP multisite installation and would like to hide the following options from the new WordPress 3.3 toolbar:

- WordPress logo
- My Sites menu
- Comments
- +New
- Search

I can successfully hide most of these items when the site admin is logged in and browsing the admin area, but not when they are browsing their actual site.

The code I am currently using is:

function change_toolbar($wp_toolbar) {
$wp_toolbar->remove_node('wp-logo');
$wp_toolbar->remove_node('my-sites');
$wp_toolbar->remove_node('comments');
$wp_toolbar->remove_node('new-content');
$wp_toolbar->remove_node('about');
$wp_toolbar->remove_node('wporg');
$wp_toolbar->remove_node('documentation');
$wp_toolbar->remove_node('support-forums');
$wp_toolbar->remove_node('feedback');
$wp_toolbar->remove_node('view-site');
}
add_action('admin_bar_menu', 'change_toolbar', 999);


Thank you

Answers (5)

2012-02-01

idt answers:

I also tried this before. AFAIK, as of 3.3 you can't hide the Admin toolbar completely. The proper way to handle is hide the items in it as what you're doing. Can you please this block of code:

function custom_admin_bar() {
global $wp_admin_bar;
$wp_toolbar->remove_node('wp-logo');
$wp_toolbar->remove_node('my-sites');
$wp_toolbar->remove_node('comments');
$wp_toolbar->remove_node('view-site');
}
add_action('wp_before_admin_bar_render', 'custom_admin_bar');


Also, there are some suggested solutions on this discussion: [[LINK href="http://wordpress.org/support/topic/hiding-admin-bar-in-wordpress-33/page/2"]]http://wordpress.org/support/topic/hiding-admin-bar-in-wordpress-33/page/2[[/LINK]]


designbuildtest comments:

Hi idt, this was unsuccessful unfortunately.


idt comments:

Hi,

All items not hidden or just some?

Thanks,
idt


idt comments:

To have "Show Toolbar when viewing site" unchecked by defaul, try this:


add_action('user_register','trash_public_admin_bar');
function trash_public_admin_bar($user_ID) {
update_user_meta( $user_ID, 'show_admin_bar_front', 'false' );
}


That works for newly registered though.


designbuildtest comments:

Hi idt, thanks for your snippet.
Works well for new users added to an existing site, but not when a new user is created when a new site is created in a multisite environment.

It seems very difficult to totally remove the new 3.3 toolbar from the frontend, thus perhaps a better solution is to try and remove selected items from the toolbar as I proposed in my initial question?

Thanks again for your help.


idt comments:

With this:
function custom_admin_bar() {
global $wp_admin_bar;
$wp_toolbar->remove_node('wp-logo');
$wp_toolbar->remove_node('my-sites');
$wp_toolbar->remove_node('comments');
$wp_toolbar->remove_node('view-site');
}

add_action('wp_before_admin_bar_render', 'custom_admin_bar');

which of the items were not hidden? Or it didn't work completely?


designbuildtest comments:

Hi idt, the whole toolbar was present, no items were hidden unfortunately.


idt comments:

Oopss... Sorry, it should have been:

function custom_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->remove_node('wp-logo');
$wp_admin_bar->remove_node('my-sites');
$wp_admin_bar->remove_node('comments');
$wp_admin_bar->remove_node('view-site');
}

add_action('wp_before_admin_bar_render', 'custom_admin_bar');


Please try that.

Thanks,
idt


designbuildtest comments:

This works for the backend toolbar, not the fronted toolbar unfortunately.
Thanks.


idt comments:

Can you please try this:

function custom_admin_bar() {
if ( !current_user_can( 'install_themes' ) ) {
global $wp_admin_bar;
$wp_admin_bar->remove_node('wp-logo');
$wp_admin_bar->remove_node('my-sites');
$wp_admin_bar->remove_node('comments');
$wp_admin_bar->remove_node('view-site');
}
}

add_action('wp_before_admin_bar_render', 'custom_admin_bar');


designbuildtest comments:

Hi idt, as per my response to John Cotton below, I'll be voting for 50% of the prize to you as

add_action('user_register','trash_public_admin_bar');

function trash_public_admin_bar($user_ID) {

update_user_meta( $user_ID, 'show_admin_bar_front', 'false' );

}


was the best non-CSS solution proposed.

Thanks

2012-02-01

John Cotton answers:

You don't tell us why you want to hide the bar, but here are two suggestions.

If you're not too bothered about the bar being in the HTML source, why not just override the CSS and hide the elements you don't what (#wp-admin-bar-wp-logo, #wp-admin-bar-site-name etc).

Alternatively, if you really want to prevent users from doing certain things (by hiding the linked) then you should properly remove access using role capabilities.

function limit_mu_admin(){
global $wp_roles;
$wp_roles->remove_cap( 'administrator', 'moderate_comments' );
// etc
}
add_action( 'admin_init', 'limit_mu_admin' );


designbuildtest comments:

Hi John, I suppose you either love or hate the new toolbar - me, I really dislike the toolbar displayed by default on the frontend.

Good suggestion re: remove_cap() - thanks.

I'd would prefer to avoid CSS hacks, but will have to resort to this method in this instance I suspect.

I'm voting 50% of the prize to you and 50% to idt as

add_action('user_register','trash_public_admin_bar');

function trash_public_admin_bar($user_ID) {

update_user_meta( $user_ID, 'show_admin_bar_front', 'false' );

}


was pretty close to what I needed.

Thanks everyone


John Cotton comments:

<blockquote>I'd would prefer to avoid CSS hacks, but will have to resort to this method in this instance I suspect.</blockquote>

I wouldn't regard this as a hack - your CSS is independent of the WP core. However, I would agree that it's not the most elegant solution since the HTML is still being sent out.

But sometimes.....

2012-02-01

Kannan C answers:

This will hide the entire admin bar from the front end.

add_filter('show_admin_bar', '__return_false');

This is what you are looking for?


designbuildtest comments:

Sorry Kannan, copied your code snippet as-is into my functions.php, but it does not remove the toolbar.

Removing the frontend toolbar would however solve my problem.

My understanding however is that the new 3.3 toolbar can't be completely removed, only amended?


Kannan C comments:

I can understand what do you mean. I am not sure that you can prevent the entire wp admin bar printing in to the admin area. You customize the nodes that you already using a code for it. But you can hide the entire bar with css style.


Kannan C comments:

Ok. adding these extra nodes to your function removed all menus except the logout link.

$wp_toolbar->remove_node('site-name');
$wp_toolbar->remove_node('new-post');
$wp_toolbar->remove_node('updates');


Kannan C comments:


function change_toolbar($wp_toolbar) {
$wp_toolbar->remove_node('wp-logo');
$wp_toolbar->remove_node('my-sites');
$wp_toolbar->remove_node('comments');
$wp_toolbar->remove_node('new-content');
$wp_toolbar->remove_node('about');
$wp_toolbar->remove_node('wporg');
$wp_toolbar->remove_node('documentation');
$wp_toolbar->remove_node('support-forums');
$wp_toolbar->remove_node('feedback');
$wp_toolbar->remove_node('view-site');
$wp_toolbar->remove_node('site-name');
$wp_toolbar->remove_node('new-post');
$wp_toolbar->remove_node('updates');
$wp_toolbar->remove_node('my-account'); //will remove the My account menu too.

}
add_action('admin_bar_menu', 'change_toolbar', 999);


Kannan C comments:

http://auroral.co.uk/2011/04/04/remove-the-wordpress-default-admin-bar/
Try install this from your mu-plugins folder.

2012-02-01

Arnav Joy answers:

go to
Go to Users > Your Profile > Show Admin Bar

uncheck option show admin bar(when vieweing site)


Arnav Joy comments:

Disable Admin Bar for everyone

This one is reasonably simple, to disable the WordPress Admin Bar for everyone, add the following to your theme's functions.php file, the first bit hides the admin bar, the second bit hides the settings:

/* Disable the Admin Bar. */
add_filter( 'show_admin_bar', '__return_false' );

<?php function yoast_hide_admin_bar_settings() {
?>
<style type="text/css">
.show-admin-bar {
display: none;
}
</style>
<?php
}

function yoast_disable_admin_bar() {
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'admin_print_scripts-profile.php',
'yoast_hide_admin_bar_settings' );
}
add_action( 'init', 'yoast_disable_admin_bar' , 9 );


designbuildtest comments:

Sorry Arnav, yes, I know you can do this... I need to have the toolbar removed/customised programmatically.


Arnav Joy comments:

check this plugin


Arnav Joy comments:

http://wordpress.org/extend/plugins/hide-toolbar-plugin/


designbuildtest comments:

Sorry Arnav, I don't believe the Yoast code works for the new 3.3 toobar.


designbuildtest comments:

Hi Arnav, I require a snippet for functions.php, rather than a plugin. thanks


Arnav Joy comments:

test this in functions.php
show_admin_bar(false);


Arnav Joy comments:

see this link

http://digwp.com/2011/04/admin-bar-tricks/


Arnav Joy comments:

try this in functions.php

remove_action(‘init’, ‘wp_admin_bar_init’);


or try this

function nlk_admin_bar_render() {
global $wp_admin_bar;
// we can remove a menu item, like the COMMENTS link, just by knowing the right $id
$wp_admin_bar->remove_menu('comments');
// we can add a submenu item too
$wp_admin_bar->add_menu( array(
'parent' => 'new-content',
'id' => 'new_media',
'title' => __('Media'),
'href' => admin_url( 'media-new.php')
) );
}
// and we hook our function via
add_action( 'wp_before_admin_bar_render', 'nlk_admin_bar_render' );


designbuildtest comments:

Hi Arnav, the last two suggestions are for older versions of WordPress. Please can you test on a 3.3 installation. Thanks.


Arnav Joy comments:

can you show me the url of your site?


Arnav Joy comments:

try this

function remove_admin_bar_links() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
$wp_admin_bar->remove_menu('view-site');
$wp_admin_bar->remove_menu('new-content');
$wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );


Arnav Joy comments:

try this in your css file
#wpadminbar { display:none; }


designbuildtest comments:

Hi Arnav, #wpadminbar { display:none; } does remove the toolbar, but leaves a white space at the top of the page. I am keen to avoid CSS hacks and customise the toolbar, rather than remove it completely. Thanks.


Arnav Joy comments:

To remove extra sapce try this
#wpadminbar
{ display: none; visibility: hidden; }

you can try this

Set visibility:hidden; to the following items.

#wp-admin-bar-wp-logo .ab-icon
#wp-admin-bar-wp-logo .ab-item
#wp-admin-bar-updates .ab-icon
#wp-admin-bar-updates .ab-item
#wp-admin-bar-comments .ab-icon
#wp-admin-bar-comments .ab-item
#wp-admin-bar-new-content .ab-icon
#wp-admin-bar-new-content .ab-item


designbuildtest comments:

Hi Arnav, as mentioned, I am keen to avoid CSS hacks and customise the toolbar programmatically, rather than remove it completely. Thanks.

2012-02-01

Fahad Murtaza answers:

Use my plugin

http://wordpress.org/extend/plugins/hide-toolbar-plugin/


designbuildtest comments:

Hi Fahd, having "Show Toolbar when viewing site" unchecked by default for all new users would solve my problem. Would your plugin enable this? Thanks.


Fahad Murtaza comments:

Yes it does that :)


Fahad Murtaza comments:

By default, it will be unchecked.

Each user has option to enable disable to their liking.


designbuildtest comments:

Hi Fahd, if possible I would like to avoid using a plugin.

I did however download your plugin, looked into the source and added the following to my functions.php:

if (!function_exists('disableAdminBar')) {

function disableAdminBar(){

remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // for the front end

function remove_admin_bar_style_frontend() { // css override for the frontend
echo '<style type="text/css" media="screen">
html { margin-top: 0px !important; }
* html body { margin-top: 0px !important; }
</style>';
}

add_filter('wp_head','remove_admin_bar_style_frontend', 99);

}

}


Unfortunately this did not work for me and the frontend toolbar is still visible.

I am keen to avoid CSS overrides/hacks if possible.

As mentioned in my last response to idt below, is it possible to remove individual items from the frontend toolbar, rather than the whole toolbar itself?

Thanks