Disable WordPress dashboard for subscribers

There’s sometimes scenarios where you want to disable the WordPress dashboard for certain users such as subscribers. You may also want to disable the admin bar for subscribers too.

This isn’t an uncommon scenario, this post tells you how to both disable the WordPress Dashboard for subscribers and disable the admin bar.

Adding the action

If you’re developing a WordPress theme, you are more than likely familiar with WordPress’ actions and hook mechanism.

We’re essentially going to add a new action to the admin init in order to achieve our objective of disabling the WordPress dashboard for subscribers. Add this to your functions.php file:

add_action('admin_init', 'disable_dashboard');

function disable_dashboard() {
    if (current_user_can('subscriber') && is_admin()) {
        wp_redirect(home_url()));
        exit;
    }
}

The above code will redirect subscribers to the home page. You could redirect to any page on your site here.

Disable WordPress dashboard for all but administrators

You may want to disable the dashboard for anyone who isn’t an administrator. You can achieve this with:

add_action('admin_init', 'disable_dashboard');

function disable_dashboard() {
    if (!is_user_logged_in()) {
        return null;
    }
    if (!current_user_can('administrator') && is_admin()) {
        wp_redirect(home_url()));
        exit;
    }
}

Notice this time I’m returning null for when users are not logged in. This is to ensure that admin-ajax.php still works for logged out users, otherwise it tries to redirect.

That’s pretty much it. Very straight forward. You could also target specific capabilities instead of for user roles, a full list can be found on the WordPress codex.

Hiding the admin bar for subscribers

Aside from disabling the dashboard, you might want to also hide the admin bar for subscribers too. Simply add the following action to your functions.php file:

add_action('admin_init', 'disable_admin_bar');

function disable_admin_bar() {
    if (current_user_can('subscriber')) {
        show_admin_bar(false);
    }
}

As with before, it is possible to switch ‘subscriber’ for any user type or capability. As with before, the WordPress Codex has a full list of capabilities to target.

That’s essentially it. If you have any questions or feedback, feel free to leave a comment below.

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

12 Responses

  1. Marion says:

    if (current_user_can(‘subscriber’) && is_admin()) {
    Why && is_admin? is he a subscriber or an admin? i don’t get the logic, can you explain it?

    • Ste says:

      Hi Marion. is_admin() is one of those WordPress functions which I feel is poorly named. is_admin() actually checks to see if you’re in the WordPress dashboard rather than being a role related check. I personally think ‘is_dashboard()’ would have been a better name.

  2. Cathy says:

    Thank you so much for this post- I’ve been looking for this information for weeks!

  3. Cathy says:

    Wow that would wonderful!! One issue I see popping up a lot is the use of username by WP and Buddypress and the inability to control if display name or full name is shown instead of username on various posts and user profiles (Maybe what I mean is a way to switch nicename from username to display name or full name?).

  4. Cathy says:

    Thanks!!!!

  5. Cathy says:

    Hope you have a great New Year! Sorry to trouble you with questions, but I am not sure if I am using this code correctly. I was testing all of my buttons and I realized that a non-admin user will still be directed to the profile section of the WP dashboard if he/she resets a password with the WP reset password link (user enters email then clicks link in email, resets password, then clicks login link to log in with new password, login takes user to WP profile). Do I need to change something to have user redirected to a custom page (home, buddypress profile, etc)?

    Thanks so much!

    Cathy

  6. Igor Skoldin says:

    You need to add a check if this is an ajax request to the disable_dashboard function, otherwise you will get the homepage html as reponse for all your frontend ajax requests.

    wp_redirect(home_url())); in the disable_dashboard function has too many braces, should be wp_redirect(home_url())

  7. Igor Skoldin says:

    You should also use the ‘init’ hook instead of ‘admin_init’ for your disable_admin_bar function if you want to disable it on the frontend.

  8. Cathy says:

    Thanks for the tips!

  9. Mark says:

    This doesn’t disable the dashboard for subscribers, it redirects them from it.

    Why reinvent features such as the profile page when you can merely hide the portions of the dashboard you don’t want the non-admins to reach?

Leave a Reply