Remove admin bar for subscribers

I have a membership site. I need to disable the admin bar for the subscribers.

I have used this code below:

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
if (!current_user_can('administrator')  !is_admin()) {
  show_admin_bar(false);
}
}

which removes the admin bar from the frontend for the subscriber, but when they go to their profile page wp-admin/profile.php, the admin bar is still showing there.

I am using Paid Membership Pro plugin which I think made the code not working on the backend for subscribers.

Also, I have used this code to remove the admin bar from everywhere:

if (!function_exists('disableAdminBar')) {

    function disableAdminBar(){

    remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );

    function remove_admin_bar_style_backend() {
      echo 'stylebody.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }/style';
    }

    add_filter('admin_head','remove_admin_bar_style_backend');

  }

}

add_filter('admin_head','remove_admin_bar_style_backend');

But this code is also not working.

I just want to remove the admin bar for the subscribers from both the frontend and backend pages.

Is there any specific code I am missing? I am using Paid membership Pro.

Thank you for helping.

Topic admin-bar membership user-roles php permissions Wordpress

Category Web


// Disable admin topbar for all roles except admins:
add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
    show_admin_bar(false);
}

}


show_admin_bar(false) doesn't work on backend pages. You'll have to remove the admin bar hooks if you want to remove them from the backend.

function remove_admin_bar_hooks() {
    
    // Removes the hooks that render the admin bar.
    remove_action('template_redirect','_wp_admin_bar_init', 0);
    remove_action('admin_init','_wp_admin_bar_init');
    remove_action('before_signup_header','_wp_admin_bar_init');
    remove_action('activate_header','_wp_admin_bar_init');
    remove_action('wp_body_open','wp_admin_bar_render',0);
    remove_action('wp_footer','wp_admin_bar_render',1000);
    remove_action('in_admin_header', 'wp_admin_bar_render', 0);
    
    // Removes the admin bar class from the body tag.
    add_filter('body_class',function($wp_classes, $extra_classes) {
        // Deletes the admin-bar class from the arrays if present.
        return array_diff( 
            array_merge( $wp_classes, (array) $extra_classes ), 
            array('admin-bar')
        );
    },10000,2);
}

You can refer to this article for more information: https://blog.terresquall.com/2021/09/remove-the-admin-bar-on-the-wordpress-admin-backend/


I had to do the same but keep the bar for editors & admins but remove for others roles. I did it like that :

in functions.php

function check_user_role($roles, $user_id = null) {
    if ($user_id) $user = get_userdata($user_id);
    else $user = wp_get_current_user();
    if (empty($user)) return false;

    foreach ($user->roles as $role) {
        if (in_array($role, $roles)) {
            return true;
        }
    }
    return false;
}

also in functions.php

// show admin bar only for admins and editors
if (!check_user_role(array('administrator','editor'))) {
  add_filter('show_admin_bar', '__return_false');
}

To hide admin bar from non admin users, add the following code to you functions.php file

// show admin bar only for admins
if (!current_user_can('manage_options')) {
    add_filter('show_admin_bar', '__return_false');
}
// show admin bar only for admins and editors
if (!current_user_can('edit_posts')) {
    add_filter('show_admin_bar', '__return_false');
}

The PMPro team made a plugin to do this: https://wordpress.org/plugins/hide-admin-bar-from-non-admins/

The includes the PHP and CSS code needed to fully hide the admin bar: https://plugins.svn.wordpress.org/hide-admin-bar-from-non-admins/trunk/hide-admin-bar-from-non-admins.php

function habfna_hide_admin_bar_settings()
{
?>
    <style type="text/css">
        .show-admin-bar {
            display: none;
        }
    </style>
<?php
}
function habfna_disable_admin_bar()
{
    if(!current_user_can('administrator'))
    {
        add_filter( 'show_admin_bar', '__return_false' );
        add_action( 'admin_print_scripts-profile.php', 'habfna_hide_admin_bar_settings' );
    }
}
add_action('init', 'habfna_disable_admin_bar', 9);

I did a quick research on this and I don't think you can with a function, as said in the codex.

Note: It is no longer possible to hide the Toolbar when viewing the Administration Screens, but users can disable it on the front-end of the site in their Profile screen.

Disabling in the frontend results in the same as you've already done.

I would suggest hiding it with css.

#wpadminbar {
    display: none;
}
html {
    padding-top: 0; // Move up the page's content by the bar's height
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.