A Script to Update WordPress User Role Capabilities

San Francisco WordPress ConsultantsHow 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:

The first 2 links above approach the problem in different ways. The first link; an answer from Stack Exchange updates the user object (and doesn’t include how to create a script for the code mentioned) and the 2nd link at Shine PHP has you editing the database directly, which in my case I could have easily done, but just seems a little messy and could really lead to some problems if fat-fingered. The Shine PHP site seems to be the same people that created and maintain the plugin User Role Editor which is nice but overpowered for this particular use case. There’s really only about 2 resources that are easily searchable to help you edit user role capabilities in WordPress. I’ve added a script to that.

Post written by Ed Reckers

Founder and lead web development consultant at Red Bridge Internet : San Francisco WordPress Developers and Consultants.

3 Responses to “A Script to Update WordPress User Role Capabilities”

  1. 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.


  2. 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!

  3. Ed Reckers

    You can run it anywhere on your webroot.

Leave a Reply