How to update WordPress user role capabilities using a simple PHP script and WordPress user functions. I came upon this issue recently in which I had full access to a WordPress installation; files, database, etc., but my WordPress user account was originally added as an Editor or some lower role by a client/third party and I really needed to update to an Administrator role. Of course without the original Administrator account I couldn’t update my own role so I needed to create a script to cleanly edit user role/capabilities. Below is a script to change a WordPress user role using the WordPress function wp_update_user:
/* * Updates user role using WordPress function wp_update_user. * * Simple script to be run at webroot. Update user_id and new_role to taste * and run as regular PHP file on command line. * * @package WordPress */ require( './wp-load.php' ); // id of user to update $user_id = 2; /* * Basic list of user roles * * administrator * editor * author * contributor * subscriber * */ // user role to update to $new_role = 'administrator'; // update user role using wordpress function wp_update_user( array ('ID' => $user_id, 'role' => $new_role ) ) ; |
You can take this and save it out as a file called manually-update-role.php or whatever you like. Open the file and update user_id and new_role and then run. Check that it ran successfully (the user role should of course be changed) and remove file. Run the file like so:
php manually-update-role.php
There are a couple pages on the web which I used as a resource:
- http://wordpress.stackexchange.com/questions/4725/how-to-change-a-users-role
- http://www.shinephp.com/how-to-change-wordpress-user-role-capabilities/
- http://wordpress.org/extend/plugins/user-role-editor/
- http://codex.wordpress.org/Function_Reference/wp_update_user
3 Responses to “A Script to Update WordPress User Role Capabilities”
Ian
Many thanks for posting this script, Ed. Very easy to use – it worked like a dream.
I did have a challenge finding the user ID though. Because of the access level I was logged in with, I couldn’t see the user ID by hovering over the list of users. However, I found it by creating a draft page and then hovering over the author in the page list this then showed me an author ID and I guessed it was the same … and it worked. I hope this tip might help somebody else.
Ian
Craig Allen
Where did you place the “php manually-update-role.php” line of code?
Thanks in advance for your help, running in to the exact same issue!
Ed Reckers
You can run it anywhere on your webroot.