wp_update_user not updating

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

http://wordpress.org/support/topic/wp_update_user-not-updating?replies=11

Read More

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

Chris

UPDATE:

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@gmail.com [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;
if(!current_user_can('edit_posts'))
{

    print '<div class="wrap"><h2>Your account has been restricted, most likely due to an unpaid subscription.</div>';
}
else
{
    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
$user->set_role('editor');
if(!$user->has_cap('edit_posts'))
{
$user->add_cap('edit_posts');
}
wp_cache_delete($unpaid->uid, 'users');

//another attempt
$uID = $unpaid->uid;
wp_insert_user(array('ID'=>$uID,'role'=>'editor')); 

UPDATE AGAIN!

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);
$new->set_role('editor');

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' );
do_action('profile_update');

Any thoughts?

Related posts

Leave a Reply

3 comments

  1. 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'] ) ) {
        upgrade_user();
    } 
    
  2. 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 );