admin管理员组

文章数量:1318325

I'm trying to avoid that subscriber users had access to admin. I'm using the following code:

add_action( 'admin_init', 'lr_no_admin_u_access', 100 );
function lr_no_admin_u_access() {
  global $current_user;
  $user_roles = $current_user->roles;
  $user_role = array_shift($user_roles);
  $redirect = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : site_url();

  if($user_role !== 'editor' && $user_role !== 'administrator'){
    wp_logout();
    wp_safe_redirect($redirect);
    exit;
  }
}

When a user navigates to '/wp-admin', should be logged out and redirected to the home page. Instead, a white page full of notices and warnings appears. Examples of errors:

Notice: Undefined offset: 2 in /home/[site]/public_html/wp-admin/includes/plugin.php on line 1911

Warning: Cannot modify header information - headers already sent by (output started at /home/[site]/public_html/wp-admin/includes/plugin.php:1911) in /home/[site]/public_html/wp-includes/functions.php on line 6270

... And many more. and many more. Why? How to avoid?

本文标签: wp adminHow to logout the current user without notices and warnings