Skip Navigation

[Resolved] Block completely backend access to a user role

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to completely restrict backend access from a specific User role.

Solution: Here's a code example showing how to restrict users in a single role from the admin:

add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );
function restrict_admin_with_redirect() {
  $user = wp_get_current_user();
    $roles = $user->roles;
    $role = array_shift($roles);
    if ( $role == 'hide-from-role-slug' && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
        wp_redirect( site_url() );
        exit;
    }
}

Replace hide-from-role-slug with the slug of the role you want to restrict.

Then you'll probably want to hide the admin bar as well. I have an example here:

add_filter('show_admin_bar', 'hide_for_roles_func');
function hide_for_roles_func(){
  if( is_user_logged_in() ){
    $user = wp_get_current_user();
    $roles = $user->roles;
    $role = array_shift($roles);
    if( $role == 'hide-from-role-slug' ){
        return false;
    } else {
      return user_can( $user, 'manage_options');
    }
  }
  return false;
 
}

Replace 'hide-from-role-slug' with the slug of the role you want to restrict.

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by stephaneG-5 4 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1311577

Tell us what you are trying to do?

I have a website where users can post a recipe. I deal with their post and registration on the frontend. So I want to block completely their access to the backend. How can we do that?

Is there any documentation that you are following?

I've searched on the forums and read that it's possible, but nothing explains how to do that.

#1311605

It's possible with custom code, but Toolset does not provide this ability out-of-the-box. Here's a code example showing how to restrict users in a single role from the admin:

add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );
function restrict_admin_with_redirect() {
  $user = wp_get_current_user();
    $roles = $user->roles;
    $role = array_shift($roles);
    if ( $role == 'hide-from-role-slug' && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
        wp_redirect( site_url() );
        exit;
    }
}

Replace hide-from-role-slug with the slug of the role you want to restrict.

Then you'll probably want to hide the admin bar as well. I have an example here:

add_filter('show_admin_bar', 'hide_for_roles_func');
function hide_for_roles_func(){
  if( is_user_logged_in() ){
    $user = wp_get_current_user();
    $roles = $user->roles;
    $role = array_shift($roles);
    if( $role == 'hide-from-role-slug' ){
        return false;
    } else {
      return user_can( $user, 'manage_options');
    }
  }
  return false;

}

Replace 'hide-from-role-slug' with the slug of the role you want to restrict.

#1311617

Thanks you!

The menu bar is hidden, but the backend is still accessible. I guess I should add a filter for the function restrict_admin_with_redirect()

Which filter should I use?

#1311619

Ah yes, sorry. Copy + paste error. You can add like this:

add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );
#1311639

My issue is resolved now. Thank you!

I think Toolset should consider adding this to the user role settings. It could be two check boxes : one for the backend acccess and the other for the admin tool bar.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.