Wordpress - How to change a user's role?
See the WP_User class, you can use this to add and remove roles for a user.
EDIT: I really should have provided more information with this answer initially, so i'm adding more information below.
More specifically, a user's role can be set by creating an instance of the WP_user class, and calling the add_role()
or remove_role()
methods.
Example
Change a subscribers role to editor
// NOTE: Of course change 3 to the appropriate user ID
$u = new WP_User( 3 );
// Remove role
$u->remove_role( 'subscriber' );
// Add role
$u->add_role( 'editor' );
Hopefully that's more helpful than my initial response, which wasn't necessarily as helpful.
Just note that there is a simpler way to change the user role which is especially helpful when you do not know the current role of the user: ->set_role()
Example:
// Fetch the WP_User object of our user.
$u = new WP_User( 3 );
// Replace the current role with 'editor' role
$u->set_role( 'editor' );
To extrapolate on t31os's answer you can slap something like this in your functions file if you want to do this programmatically based on a condition
$blogusers = get_users($blogID.'&role=student');
foreach ($blogusers as $user) {
$thisYear = date('Y-7');
$gradYear = date(get_the_author_meta( 'graduation_year', $user->ID ).'-7');
if($gradYear < $thisYear) {
$u = new WP_User( $user->ID );
// Remove role
$u->remove_role( 'student' );
// Add role
$u->add_role( 'adult' );
}
}