While I quite like the admin bar I actually want it to be OFF by default instead of ON ( I don't want to disable it altogether because I want users to be able to turn it on if they want - but but I don't want to have to manually turn it off for every user ) Is there a way to implement this.
3 Answers
add_action("user_register", "set_user_admin_bar_false_by_default", 10, 1);
function set_user_admin_bar_false_by_default($user_id) {
update_user_meta( $user_id, 'show_admin_bar_front', 'false' );
update_user_meta( $user_id, 'show_admin_bar_admin', 'false' );
}
Place in theme functions file or you can make into a plugin.
Once user registers it will go and set the users admin bar prefs to false. The user can then, once logged in, set this to true.
-
5why don't use
update_user_meta()
function? Like so:update_user_meta( $user_id, 'show_admin_bar_front', 'false' );
– MamadukaCommented Sep 23, 2011 at 12:55 -
2double that. +1 for @Brady Should work seamless, even when nicer with Mamadukas addition. Btw: This one should become a WPSE plugin ;)– kaiserCommented Sep 23, 2011 at 12:59
-
Yup you are absolutely right. I was in a hurry to post and didn't research alternate methods. Ill update answer to use this instead.– ScottCommented Sep 23, 2011 at 13:02
-
sounds like it will work and be an interesting bit of code - thanks - i cant try it out till monday though.... edit ... blimey never heard of WPSE plugins before , what a resource will have to have a good look Commented Sep 23, 2011 at 17:00
-
1wordpress.org/extend/plugins/admin-bar-default-off can now get this as a plugin. Nice to see someone has already down voted my plugin, beyond me why...– ScottCommented Sep 26, 2011 at 11:16
function wpse29210_admin_bar_toogle()
{
add_filter( 'show_admin_bar', '__return_false' );
$user = get_userdata( $GLOBALS['current_user'] )->data->ID;
if ( ! is_admin() && $user->show_admin_bar_front )
add_filter( 'show_admin_bar', '__return_true' );
if ( is_admin() && $user->show_admin_bar_admin )
add_filter( 'show_admin_bar', '__return_true' );
return;
}
add_action( 'init', 'wpse29210_admin_bar_toogle' );
-
I thought that was the code to get rid of it altogether ? Commented Sep 22, 2011 at 16:03
-
-
Thanks for your help but I'm not sure what that code is meant to do. What I want is for wordpress to have "Show Admin Bar when viewing site" unchecked by default when a new user is setup. Commented Sep 22, 2011 at 16:20
-
It's meant to disable the admin bar per default. If a user saves his profile with one of the two fields checked, then the admin bar is shown. I'm sorry, but I guess you won't be able to uncheck per default unless you add some js to the
admin_head
to disable it.– kaiserCommented Sep 22, 2011 at 16:47 -
1Maybe you can hook into the account creation process and set
show_admin_bar_front
andshow_admin_bar_admin
to false? Just an idea– ScottCommented Sep 22, 2011 at 16:55
This finally works:
wp_update_user( array ( 'ID' => $user_id, 'show_admin_bar_front' => 'false' ) ) ;