How to reset user roles in WordPress

If your website is redirecting back to the main page after logging in to the dashboard, it is confirmed that there are some issues with permissions assigned for your logged-in user. This can be a result of a problematic plugin or role-editors/managers you have installed. Luckily, we have wp-cli tools that come in handy in such situations. Let’s see how to Reset user roles in WordPress to fix the redirection issues.

Reset user roles in WordPress

We are checking the roles assigned to a user and Reset user roles in WordPress if necessary through wp-CLI tools.

wp-cli

Reset user roles in WordPress

wp-cli is the command-line interface for WordPress. You can use it to update plugins, configure multisite installations, and for many actions, you might perform in the WordPress admin.

First, check the list of users using wp-cli command to see the details of the user and roles assigned.

$ wp user list
+----+--------------+-----------------+----------------------------+---------------------+-------+
| ID | user_login | display_name | user_email       | user_registered     | roles |
+----+--------------+-----------------+----------------------------+---------------------+-------+
| 1 | jospeh      | jospeh       | [email protected]   | 2018-03-01 23:56:12 | |
| 4 | gilbert     | gilbert      | [email protected]  | 2020-10-01 00:27:09 | |
| 3 | ramson      | ramsonndo    | [email protected]| 2020-09-01 16:29:00 | |
+----+--------------+-----------------+----------------------------+---------------------+-------+

In this example, you can see three users but their roles are not listed or null. Some plugins have reset or corrupted their roles.

We can simply fix this by running the command as below.

$ wp user set-role 1 administrator
Success: Added jospeh (1) to xyz as administrator.

Where 1 is the user ID we got from the wp user list command. We can substitute the IDs of the user for which you want to reset the roles.

$ wp user list
+----+--------------+-----------------+----------------------------+---------------------+-------+
| ID | user_login | display_name | user_email       | user_registered     | roles |
+----+--------------+-----------------+----------------------------+---------------------+-------+
| 1 | jospeh      | jospeh       | [email protected]   | 2018-03-01 23:56:12 | administrator
| 4 | gilbert     | gilbert      | [email protected]  | 2020-10-01 00:27:09 | |
| 3 | ramson      | ramsonndo    | [email protected]| 2020-09-01 16:29:00 | |
+----+--------------+-----------------+----------------------------+---------------------+-------+

If we run the list command again, we can see the role is now assigned to the user.

Sometimes, we encounter an error as below where the role administrator is corrupted or doesn’t exists.

$ wp user set-role 1 administrator
Error: Role doesn't exist: administrator

In that case, we need to reset the role itself before assigning it to the user.

$ wp role reset administrator
Restored 0 capabilities to and removed 0 capabilities from 'administrator' role.
Success: Role reset.

Once done, run the set-role command again. It will run without any issues.

By this simple technique using wp-cli tools, we can regain control over the dashboard for the user by doing Reset user roles in WordPress. Once logged in, it will be better to find what plugin caused this by reviewing the last actions or updates made.

Happy blogging!!!

Have you noticed the new LiteSpeed Cache plugin. This is especially useful for the people who use LiteSpeed WebServer to leverage the server-side caching features it.