Hide Admin Bar For Users

Until recently my Admin Bar was hidden for all Users on all sub-domains. Now it is showing on
https://chat.christiangays.com/
https://trans.christiangays.com/
https://intersex.christiangays.com/

The other main domain and sub-domains are good.
I have the following code in my child theme functions.php

// Begin - Show Toolbar when viewing site - make default unticked
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' ); }
// End - Show Toolbar when viewing site - make default unticked

// Begin - Admin Bar - remove for Users (using this now instead of above)
if(!current_user_can('administrator')) {
if(is_admin_bar_showing()) {
show_admin_bar(false);
}
}

I have tried installing several different plugins to "hide admin bar" but none of them have any effect.

Would appreciate any help.
Support access is granted.

  • James Morris
    • WordPress Enthusiast

    Hello Mary,

    I hope you are well today.

    I've done some testing on my own dev install (to avoid any PHP errors on your sites :wink: ) and I've come up with the following that should work nicely.

    Please replace your old code in functions.php with the following:

    // Hide Admin Bar for All Users Except Adminministrators
    if ( ! current_user_can( 'manage_options' ) ) {
    	add_filter('show_admin_bar', '__return_false');
    }

    In my tests, this disabled the admin bar for everyone except administrators.

    Please let us know if this resolves your issue.

    Best regards,

    James Morris

  • Ash
    • WordPress Hacker

    Hello Mary

    Are they subsite from a multisite or are they separate wordpress install? In either case, try to add the code as muplugin.

    Also, try the following code (little modified):

    // Hide Admin Bar for All Users Except Adminministrators
    if ( ! current_user_can( 'manage_options' ) ) {
    	add_filter('show_admin_bar', '__return_false', 99999);
    }

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    Hi Mary

    All three sites are working fine for me, admin bar was not showing in any of those sites. I was logged in with a subscriber account. Would you please make sure that you were not logged in using an admin account by any chance?

    Let us know.

    Have a good day!

    Cheers
    Ash

  • James Morris
    • WordPress Enthusiast

    Hello Mary,

    By default, the Admin bar should not be viewable by non-logged-in users. This is not something built into GeneratePress either. This would have to be enabled by a plugin or a code function added to your theme.

    The following modified code should remove this as well, however, you may want to poke around in your functions.php and plugins to see what is adding the admin bar to non-logged in users.

    // Hide Admin Bar for All Users Except Adminministrators
    if ( ! current_user_can( 'manage_options' ) && ! is_user_logged_in() ) {
    	add_filter('show_admin_bar', '__return_false', 99999);
    }

    Best regards,

    James Morris

  • Mary
    • Wisp of the WP Loop

    Thank you James. That did not fix it. Re finding something else in my functions.php file, this was not a problem before. The only other code I have, that has been there for a long time is:

    // Begin - Show Toolbar when viewing site - make default unticked
    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' ); }
    // End - Show Toolbar when viewing site - make default unticked

    I tried deleting it but it didn't help.

  • James Morris
    • WordPress Enthusiast

    Hello Mary,

    Would you please grant me access to your server so I may look at your theme files a little closer and see if I can resolve this for you?

    Please visit the Contact page and complete the form with the following information:

    Subject: "Attn: James Morris"

    In the Message box, please provide the following:

    - link back to this thread for reference
    - any other relevant urls

    - Admin login:
    Admin username
    Admin password
    Login url

    - Hosting Control Panel Login
    Admin username
    Admin password
    Login url

    - FTP credentials
    host
    username
    password
    (and port if required)

    Best regards,

    James Morris

  • Ash
    • WordPress Hacker

    Hello Mary

    Thanks for sending details, I believe James will check as soon as he is online. But before that, let's try another simple workaround. Try the following code:

    add_action( 'wp_head', function() {
            if( ! is_super_admin() || ! is_user_logged_in() )
            {
                    ?>
                    <style>
                            #wpadminbar{display: none !important;}
                    </style>
                    <?php
            }
    } );

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Mary
    • Wisp of the WP Loop

    Thank you Ash. That did not work either, but I am thinking that this may just be a symptom of a bigger problem because the Global Site Search widget shows up on all the other sites, but not on intersex and trans, and they are the two sites that I can't get rid of the Admin bar for.

  • James Morris
    • WordPress Enthusiast

    Hello Mary,

    The problem we were running into is you had installed WP Rocket on your primary domain and it was having an effect on your subsites as well. After clearing the cache the changes I've made to get rid of the admin bar have taken effect. Also, global search started displaying.

    Best regards,

    James Morris

  • James Morris
    • WordPress Enthusiast

    Hello Mary,

    In functions.php I added the following:

    // Hide Admin Bar for All Users Except Adminministrators
    if ( !current_user_can( 'manage_options' ) || !is_user_logged_in() ) {
    	add_filter('show_admin_bar', '__return_false', 99999);
    }

    This seems to be working well across your network.

    Best regards,

    James Morris :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.