1

There are heaps and heaps of membership plugins out there, all of them focusing on restricting site content based on the users chosen subscription.

I would however only want the user role to change from "have paid" to "haven't paid" when the subscription time runs out, still allowing them to view all the site content but only limited to the role capabilities I've set previously.

EDIT (for clarification): My users have a public profile page on which they present themselves and their services. They pay annually to stay public, but if they haven't payed they should be hidden from the site, i.e change role to "invisible" instead of "public". Today I do this manually by keeping dates on each user and change their role when needed to, which is quite tedious.

2
  • When you say "invisible to the site," do you mean just that the profile page is no longer "public," or do you mean anything more far-reaching?
    – CK MacLeod
    Oct 20, 2016 at 15:49
  • Precisely, the profile page should be hidden in search results, lists and by permalink until the membership is renewed.
    – talven
    Dec 4, 2016 at 8:59

1 Answer 1

1

Presumably you've saved an expiration data somewhere, so setup a wp_cron job to run daily. That job should check the expiration dates, and change the roles for users whose dates are past.

if ( ! wp_next_scheduled( 'alter_user_role_hook' ) ) {
  wp_schedule_event( strtotime('tomorrow'), 'daily', 'alter_user_role_hook' );
}
function alter_user_role_function() {
  global $wpdb;
  $today = date('Y-m-d H:i:s',strtotime('today'));
  $expired = $wdpb->get_col("SELECT user_id FROM {$wpdb->usermeta} WHERE expired_key < {$today}");
  if (!empty($expired)) {
    foreach ($expired as $uid) {
      wp_update_user( 
        array ( 
          'ID' => $uid, 
          'role' => 'havent_paid'
        ) 
      );
    }
  }
}
add_action( 'alter_user_role_hook', 'alter_user_role_function' );

I am assuming that the expiration date is stored in user meta and I am guessing at the role slug.

Untested but that should be close. I would alter the time components so that it runs at some low traffic time on your site.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.