1

My issue was the adminbar started to disappear for users who weren't the administrator. I have found a work-around ( below code ) that checks if they're logged in just before the closing </head> tag.

Has anyone come across a similar problem and found the root cause, or is there a better-practice way of hiding the adminbar?

<?php
if ( is_user_logged_in() ) {
    show_admin_bar( true );
}
elseif ( ! is_user_logged_in() ) {     
    show_admin_bar( false );
}
?>
</head>
<body <?php body_class(); ?>>
  • sounds like you have a CSS problem or some bad plugin. If I am right it is hard to predict what (if anything) will fix it. I suggest you try to find the problem and fix it before trying to use hacks to fix it – Mark Kaplun Jun 10 '16 at 16:52
  • Does your site allow http and https? WP auth cookies are usually set on a specific protocol. Its possible for users to be logged in on one, e.g https, but not the other. – user42826 Jun 10 '16 at 17:01
  • @Mark Kaplun thanks. Pretty sure it's not css. The plugins are all pretty standard popular ones. But I'll check against them. Good one. – sjingo Jun 10 '16 at 18:10
  • @user42826 . You might be on to something there. As it would explain some dodgy login behaviors too. The server guy did add redirect to https. I'll check back with him on that. THANKS! – sjingo Jun 10 '16 at 18:13
  • yes, https/http will explain things. Make sure logged in users are always served https, and by extension if that is the reason your code will not solve it as login detection will be off – Mark Kaplun Jun 10 '16 at 18:31
1

There's actually a show_admin_bar hook where you can always return true or false. I haven't run across an issue where the admin bar is randomly disappearing though. Usually on my websites I like to turn it off for admins:

/**
 * Remove Admin Bar For Administrators
 * Or anyone who can activate plugins
 */
function theme_hide_admin_bar() {
    return ( is_user_logged_in() && ! current_user_can( 'activate_plugins' ) ); // returns true or false
}
add_filter( 'show_admin_bar', 'theme_hide_admin_bar' );

OR you can permanently turn it off by just adding the following:

add_filter( 'show_admin_bar', '__return_false' );

Always show the admin bar:

add_filter( 'show_admin_bar', '__return_true' );
0

I have 2 choices for you. Both of which you place this code in your theme's function.php file.

// Disable WordPress Admin Bar for all users but admins.
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar(){
    if(!current_user_can('administrator') && !is_admin()){
        show_admin_bar(false);
    }
}

OR

// Hide admin bar from all users
show_admin_bar(false);
  • Thanks Dan. I'll try those out and feed back on the outcome. Better in the functiins file than the header. Thanks – sjingo Jun 10 '16 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.