Error in class-wp-roles.php - "array_keys() expects argument 1 to be an array"
P粉253518620
2023-08-30 13:11:16
<p>A few days ago I noticed that I could no longer access the admin dashboard in WordPress. I would log in using my normal admin username/password, but then I would be redirected to the website homepage instead of the WordPress dashboard. And the admin bar at the top of the page doesn't show the usual options for going to the dashboard or editing pages, etc. When I then try to go to wp-admin I get an error message "Sorry, you are not allowed to access this page."</p>
<p>When I enable wp_debug, I start seeing the following error message: </p>
<blockquote>
<p>Warning: array_keys() expects argument 1 to be an array, the string given in /var/www/wp-includes/class-wp-roles.php line 291</p>
<p>Warning: Invalid argument supplied to foreach() in /var/www/wp-includes/class-wp-roles.php on line 291</p>
<p>Warning: array_keys() expects argument 1 to be an array, the string given in /var/www/wp-includes/class-wp-roles.php line 291</p>
<p>Warning: Invalid argument supplied to foreach() in /var/www/wp-includes/class-wp-roles.php on line 291</p>
</blockquote>
<p>This error shows up even if I disable all plugins, and it shows up no matter which theme is currently active. I couldn't find much information about this error in my searches, and several threads about class-wp-roles are specific to certain plugins I don't use. </p>
<p>I did the normal first checks like turning off all plugins, checking wp-config.php, checking file permissions, checking account roles/permissions in phpmyadmin, and 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 can’t access WordPress dashboard. wp-admin.php. </p>
<p>Some additional information, my website uses the Storefront theme and WooCommerce, but since this error shows up even when both themes are not activated, it shouldn't matter, right? My website is hosted on GoDaddy and when I first encountered this error the website was using WordPress 6.0.3 but yesterday it automatically upgraded to 6.1.1 and I'm still having the same issue. (I really hope the upgrade fixes this problem.) Everything else about the site seems to be working fine. I just can't access the admin dashboard! Any help on this would be greatly appreciated. </p>
Wow, that’s so fast. I created a Stack Overflow account today just to post this question, and after days of banging my head against my desk trying to figure this out, I had a solution within a few hours.
For anyone experiencing similar issues, be sure to do a regular check to troubleshoot these issues first, then go to your site's phpmyadmin and then to the wp_options > table. Find the wp_user_roles entry. Make sure it has something like this:
My wp_user_roles actually just sets it to the value:
It turns out this is the wrong value! I don't know how this happened. Before this question, I had not messed around in phpmyadmin at all. It should be a plug-in or something. When I replaced that AOK with the correct list of roles and features, I was immediately able to access the WordPress admin dashboard again.
If you are using WooCommerce, there is an extra step: you need to deactivate and reactivate the WooCommerce plugin. This is because the list of roles above is missing a WooCommerce-specific role called "Store Manager", which gives you access to WooCommerce's product, orders, analytics, coupons, and more pages.
That's it! Big thanks to @VijayHardaha for helping with this! I hope this post helps someone else one day.