1

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 '<style>body.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.

-1

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
}
| improve this answer | |
2

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);
| improve this answer | |
1

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');
}
| improve this answer | |
0

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');
}
| improve this answer | |

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.