wp_update_user not updating

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();
} 

Leave a Comment