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