1

Currently on my website I have two roles created - Trial and Expired. When a user registers it defaults them to trial. I want to run a function that will automatically change their role to expired after two weeks. This is the code I have currently, but am missing something:

add_action( 'wp_login', function($username, $user) {

   $toExpire = [ 'trial'];

   $toRemove = array_intersect($user->roles, $toExpire);

   if (count($user->roles) < 2 || empty($toRemove)) {
     // nothing to do if the user has just 1 role or none of the roles you want to expire
     return;
   }


   $downloadTimestamp = get_last_download_timestamp_for_user($user->ID);

   $dayLimit = 30 * DAY_IN_SECONDS; // expire after 30 days

   if ((time() - $dayLimit) < $paymentTimestamp) {
     // do nothing if not enough time passed
     return;
   }

   if (! array_diff($toExpire, $toRemove)) {
     // if an user has ONLY roles that might expire, keep the first role
     array_shift($toRemove);
   }
}

We currently have the Ultimate Member plugin installed which doesn't give us this functionality in the plugin. I tried Expire users which doesn't work either :(

Any help is appreciated!

Thanks, A

1
  • Setting the day-limit to 30 days out each time the function is called is the problem.. I would compare the current date date() in php & compare with the date the user registered: wordpress.stackexchange.com/questions/77876/… then re-assign the user-role based on that.
    – admcfajn
    Commented Feb 15, 2018 at 17:11

0

Your Answer

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

Browse other questions tagged or ask your own question.