Change the user role after x days

Here is an idea that you can implement. I think it will work just fine.

  1. Save the expiration time in user meta. Say the meta name is change_role. What you save in the meta is unix time. If you want to change them back in 14 days. Set the meta value to time() + 60 * 60 * 24 *14

code example update_user_meta($user_id, 'change_role', time() + 60 * 60 * 24 *14);

  1. Register a wp cron to search for expired accounts and change their roles back. Run the cron every hour or so. The search query will search for users who have the change_role meta value less then the current unix timestamp value.

Code Example

The cron function may look like this.

function change_expired_users_role(){
    $args = array(
        'meta_key' => 'change_role',
        'meta_value' => time(),
        'meta_compare' => `<=`,
        'fields' => array('ID')
    );

    $users = get_users($args);

    if(empty($users))
        return;

    foreach($users as $user){
       // change user role
       // delete the user meta
    }
}

Hope this helps.