wordpress - Error in class-wp-roles.php - "array_keys() expects parameter 1 to be array"
A few days ago I noticed that I could no longer access the admin dashboard in WordPress. I would log in with my normal admin username/password but then I get redirected to the website homepage instead of the WordPress dashboard. And the admin bar at the top of the page did not display the usual options to go to the dashboard or edit the page, etc. When I then tried to go to wp-admin after that, I got an error saying "Sorry, you are not allowed to access this page."
When I enabled wp_debug, I started seeing the following error message:
Warning: array_keys() expects parameter 1 to be array, string given in /var/www/wp-includes/class-wp-roles.php on line 291
Warning: Invalid argument supplied for foreach() in /var/www/wp-includes/class-wp-roles.php on line 291
Warning: array_keys() expects parameter 1 to be array, string given in /var/www/wp-includes/class-wp-roles.php on line 291
Warning: Invalid argument supplied for foreach() in /var/www/wp-includes/class-wp-roles.php on line 291
This error displays even when I have disabled all plugins, and it displays no matter which theme is currently active. I couldn't find much about this error in my searching, and the few threads about class-wp-roles are specific to certain plugins which I don't use.
I did the normal first-checks, like turning off all my plugins, checking wp-config.php, checking file permissions, checking the account role/privileges in phpmyadmin and I even created a new admin user using phpmyadmin. I also loaded the site in "Safe mode" which disables all plugins and defaults to the "Twenty twenty" theme in WordPress. Still no luck accessing the WordPress dashboard ie. wp-admin.php.
Some additional information, my site uses the Storefront theme and WooCommerce, but since this error displays even when both of those are not active, that shouldn't matter, right? My site is hosted on GoDaddy and when I first encountered this error, the site was using WordPress 6.0.3 but yesterday it automatically upgraded to 6.1.1, but I'm still having the same problem. (I was really hoping the upgrade would fix this issue.) Everything else with the website seems to be working fine. I just cannot access the admin dashboard! Any help with this is greatly appreciated.
Answer
Solution:
Wow that was fast. I basically created a Stack Overflow account today just to post this question, after days of banging my head against the desk trying to figure this out, and I got the solution in a few hours.
For those who are experiencing a similar issue, definitely go through the regular checks to rule those out first, but then, go to phpmyadmin for your site and then the wp_options table. Look for the wp_user_roles entry. Make sure it has something like this:
a:5:{s:13:"administrator";a:2:{s:4:"name";s:13:"Administrator";s:12:"capabilities";a:61:{s:13:"switch_themes";b:1;s:11:"edit_themes";b:1;s:16:"activate_plugins";b:1;s:12:"edit_plugins";b:1;s:10:"edit_users";b:1;s:10:"edit_files";b:1;s:14:"manage_options";b:1;s:17:"moderate_comments";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:12:"upload_files";b:1;s:6:"import";b:1;s:15:"unfiltered_html";b:1;s:10:"edit_posts";b:1;s:17:"edit_others_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:10:"edit_pages";b:1;s:4:"read";b:1;s:8:"level_10";b:1;s:7:"level_9";b:1;s:7:"level_8";b:1;s:7:"level_7";b:1;s:7:"level_6";b:1;s:7:"level_5";b:1;s:7:"level_4";b:1;s:7:"level_3";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:17:"edit_others_pages";b:1;s:20:"edit_published_pages";b:1;s:13:"publish_pages";b:1;s:12:"delete_pages";b:1;s:19:"delete_others_pages";b:1;s:22:"delete_published_pages";b:1;s:12:"delete_posts";b:1;s:19:"delete_others_posts";b:1;s:22:"delete_published_posts";b:1;s:20:"delete_private_posts";b:1;s:18:"edit_private_posts";b:1;s:18:"read_private_posts";b:1;s:20:"delete_private_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"read_private_pages";b:1;s:12:"delete_users";b:1;s:12:"create_users";b:1;s:17:"unfiltered_upload";b:1;s:14:"edit_dashboard";b:1;s:14:"update_plugins";b:1;s:14:"delete_plugins";b:1;s:15:"install_plugins";b:1;s:13:"update_themes";b:1;s:14:"install_themes";b:1;s:11:"update_core";b:1;s:10:"list_users";b:1;s:12:"remove_users";b:1;s:13:"promote_users";b:1;s:18:"edit_theme_options";b:1;s:13:"delete_themes";b:1;s:6:"export";b:1;}}s:6:"editor";a:2:{s:4:"name";s:6:"Editor";s:12:"capabilities";a:34:{s:17:"moderate_comments";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:12:"upload_files";b:1;s:15:"unfiltered_html";b:1;s:10:"edit_posts";b:1;s:17:"edit_others_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:10:"edit_pages";b:1;s:4:"read";b:1;s:7:"level_7";b:1;s:7:"level_6";b:1;s:7:"level_5";b:1;s:7:"level_4";b:1;s:7:"level_3";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:17:"edit_others_pages";b:1;s:20:"edit_published_pages";b:1;s:13:"publish_pages";b:1;s:12:"delete_pages";b:1;s:19:"delete_others_pages";b:1;s:22:"delete_published_pages";b:1;s:12:"delete_posts";b:1;s:19:"delete_others_posts";b:1;s:22:"delete_published_posts";b:1;s:20:"delete_private_posts";b:1;s:18:"edit_private_posts";b:1;s:18:"read_private_posts";b:1;s:20:"delete_private_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"read_private_pages";b:1;}}s:6:"author";a:2:{s:4:"name";s:6:"Author";s:12:"capabilities";a:10:{s:12:"upload_files";b:1;s:10:"edit_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:4:"read";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:12:"delete_posts";b:1;s:22:"delete_published_posts";b:1;}}s:11:"contributor";a:2:{s:4:"name";s:11:"Contributor";s:12:"capabilities";a:5:{s:10:"edit_posts";b:1;s:4:"read";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:12:"delete_posts";b:1;}}s:10:"subscriber";a:2:{s:4:"name";s:10:"Subscriber";s:12:"capabilities";a:2:{s:4:"read";b:1;s:7:"level_0";b:1;}}}
My wp_user_roles literally just had this set as the value:
AOK
It turns out that's the wrong value! I have no idea how that happened. Before this issue, I was not messing around in phpmyadmin at all. It must have been a plugin or something. When I replaced that AOK with the correct list of roles and capabilities, I was immediately able to access the WordPress admin dashboard again.
If you are using WooCommerce, there is one additional step: You need to de-activate and then re-activate the WooCommerce plugin. This is because that list of roles above is missing a role specific to WooCommerce called "Shop Manager" which gives you access to WooCommerce's Products, Orders, Analytics, Coupons, etc pages.
That was it! Huge props to @VijayHardaha for their help in resolving this issue! I hope this thread helps somebody else out someday.
Source