In desperation I am asking for help in this forum too - if someone (ANYONE!) could take a look a this post and see if they could help I'd be eternally grateful


Basically - I am updating a user's role and capabilities via script but the changes only come in to effect (ie the user can see specific menu items) when I go into that user and hit "save"

Thanks in anticipation!!



Maybe this helps solve it?

This is the WP_User object I have echoed out on screen when logged in as the newly changed user:

WP_User Object ( [data] => stdClass Object ( [ID] => 130 [user_login] => test [user_pass] => $P$BuHO1ABLCNQ716tktgyes4jqqkfVxG. [user_nicename] => test [user_email] => [email protected] [user_url] => [user_registered] => 2012-07-19 12:07:52 [user_activation_key] => [user_status] => 0 [display_name] => test ) [ID] => 130 [caps] => Array ( [editor] => 1 ) [cap_key] => wp_capabilities [roles] => Array ( [0] => editor ) [allcaps] => Array ( [upload_files] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [manage_options] => 1 [view_menu] => 1 [editor] => 1 ) [filter] => )

This is how the menu item is being created in my plugin file (which should display to the user):

add_menu_page('Welcome', 'Welcome','edit_posts', 'welcome', 'welcome_page', get_bloginfo('template_url').'/images/icon.png', 0);

This is the function / page that the menu item returns:

function welcome_page()
    global $currrent_user;

    print '<div class="wrap"><h2>Your account has been restricted, most likely due to an unpaid subscription.</div>';
    include 'welcome-page.php';

As you can see - the user only needs edit_posts capability to view the menu item and for the function to return the welcome page. The user indeed has this capability, but cant do either of these things - unless i click "save" as admin in the user-edit page??

Edit 2

The following are all the different approaches I have taken to change the user role - if it helps!

//using this currently
$user = new WP_User($unpaid->uid);//$unpaid->uid is the users ID
wp_cache_delete($unpaid->uid, 'users');

//another attempt
$uID = $unpaid->uid;


I have just tried this and again, the db is updated, even shows 'editor' in the admin panel, but the user still cant see the appropriate menu items unless I click "update" on their profile!

$new = new WP_User($current_user->ID);

wp_cache_delete( $new->ID, 'users' );
wp_cache_delete( $new->user_login, 'userlogins' );
wp_cache_delete( $new->user_email, 'useremail' );
wp_cache_delete( $new->user_nicename, 'userslugs' );

Any thoughts?

  • I have tried everything I know of here guys - any help would be much appreciated!
    – Clearmedia
    Commented Aug 7, 2012 at 1:17
  • Are you using another plugin such as User Role Editor by any chance? I'm looking at doing something similar to you but haven't got round to it yet. My early investigations have left me with a note to look into the wp_capabilities values. It's been a while so I can't remember why but, I've added this code snipped to my notes which could help you? update_user_meta($user_id, 'wp_capabilities', '');
    – TomC
    Commented Nov 11, 2013 at 6:48
  • If the user flushes browser cache does this show the menu? Also if supercache etc are disabled, or both, does this help?
    – Tom J Nowell
    Commented Jan 20, 2014 at 1:10
  • Also does changing the capability required on the menu to the editor role rather than edit_posts help?
    – Tom J Nowell
    Commented Jan 20, 2014 at 1:13
  • 1
    I have the exact same issue with a normal wp_user_update of the display_name. I disabled my caching solutions and nothing changes. If I go to the profile page of the user, the display name is updated, but the website shows it only after I hit the Update button in user-edit.php Commented Apr 11, 2018 at 13:26

3 Answers 3


Im using remove_role and then add_role to upgrade a user from one role to another.

Here is a function that checks all user within the user role subscriber and upgrade them to editor every hour.

* Add a cron job that will update
* Subscribers to editors. Runs hourly

add_action('check_user_role', 'upgrade_user');

function run_check_user_role() {
    if ( !wp_next_scheduled( 'check_user_role' ) ) {
        wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'check_user_role');
add_action( 'wp', 'run_check_user_role' );

function upgrade_user() {

    // Get users in subscriber role
    $args = array(
        'role'  =>  'subscriber',

    $users = get_users( $args );

    foreach ( $users as $user ) {

        $user = new WP_User( $user->ID );

        // Remove current subscriber role
        $user->remove_role( 'subscriber' );

        // Upgrade to editor role
        $user->add_role( 'editor' );


Here is a way so you can try to upgrade the users manually:

Run this as http://yourdomain.com/?upgrade_user

if ( isset( $_REQUEST['upgrade_user'] ) ) {
  • Tried that one Pontus - no good im afraid - my mind is slowly turning to mush - this is the one tiny fly in an otherwise cracking system!
    – Clearmedia
    Commented Jul 18, 2012 at 6:12
  • Thats weird, im running this on a newly created WordPress 3.4 install and it works for me. Where do you add it? and do you have something like this function already? Commented Jul 18, 2012 at 11:37
  • You can see my update above - I run the code in my plugin file. The database updates correctly - but the user still cant see the menu items, unless I log in as admin, go to users > edit the user and simply click "save"...
    – Clearmedia
    Commented Jul 19, 2012 at 4:43
  • You should add global $current_user to check the capabilities of the user, i have updated your code. you can check on a role instead like this: if ( $user->has_cap('edit_posts') ) {} Commented Jul 19, 2012 at 9:00
  • Sorry Pontus - you've updated my code? I have included another edit to my initial post - I have tried has_cap. Am I wrong in assuming I don't need to declare global $current_user if I already have the user's ID?
    – Clearmedia
    Commented Jul 19, 2012 at 11:31

I better method is. Which uses WP_User to toggle/set the role.

$role = 'editor';
$user = new WP_User($user_id);
  • 2
    Since this has been -3'ed, can someone advise why my solution is wrong? Commented Jul 15, 2012 at 17:17
  • Thanks for the responses guys - have tried both these approaches but to no avail :( I believe the system is updating the database correctly but the user still can't access the applicable menu items until I go into that user and click save...?
    – Clearmedia
    Commented Jul 17, 2012 at 22:32
  • Try adding wp_cache_delete($user_id, 'users'); after my $user->set_role. But before doing that. Does the user get shown the correct menu items, if they logout and in again? If so the WP temp cache is interfering and wp_cache_delete will clear this. (Have not tested) Commented Jul 17, 2012 at 22:53
  • Thanks Barry - they still cant see the menu items after logout and log back in again. - it's like the updating function is missing or not doing one small something that occurs when updating a user profile via the admin > users panel...?
    – Clearmedia
    Commented Jul 18, 2012 at 6:09
  • 2
    This answer is correct, I tested it, it works properly. I don't know what you're doing wrong, but there is literally nothing more to setting new roles than this.
    – Otto
    Commented Aug 26, 2012 at 14:31

If you use other plugins you need to read the documentation on how to set role. In my case, I used Ultimate Member so I have to add these lines:

UM()->roles()->set_role( $user_id, $user_role );
//and then clear the user cache after updating the role:
UM()->user()->remove_cache( $user_id );
  • This worked for me. As suggested by Son, we also need to check the plugin which handles access control for the menu display. In my case, it was Ultimate Member plugin and the above code worked perfectly.
    – Vikas NS
    Commented Apr 3, 2021 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.