0
//Auto Delete Expired Users

function delete_users(){
   $bloguser = get_users( array( 'role' => 'subscriber' ) );
   foreach ( $bloguser as $user ) {
       if ($user->user_url == 'http://day' and time() - $user->user_registered > 1){
           wp_delete_user($user->ID); 
       }
   }
}
add_action('auto_delete_users','delete_users');
4
  • auto_delete_users isn't a core WordPress hook, where have you created it? Oct 16, 2017 at 1:48
  • in my function.php file. Oct 16, 2017 at 7:46
  • I added this code to in my function.php file. I edited the 'auto_delete_users' and replaced it with 'wp_footer'. Now the function is called everytime the footer loads. Oct 16, 2017 at 7:52
  • Doing some research here I found out my code was missing: require_once(ABSPATH.'wp-admin/includes/user.php' ); so I added it and it worked. Oct 16, 2017 at 7:53

1 Answer 1

0

For the benefit of those who will come across this question, I have solved it. This is how the functioning code works. Now this code helps me to delete Subscribers who have registered on the blog using a plan of their choice, I am working on a paid blog and figured I could do it all by myself without having to buy a plugin.

//Auto Delete Expired Users
function delete_users(){
    require_once(ABSPATH.'wp-admin/includes/user.php' );
   $bloguser = get_users( array( 'role' => 'subscriber' ) );
   foreach ( $bloguser as $user ) {

   //Monthly Subscribers
   if ($user->user_plan == 'month' and date('Y-m-d H:m:s') - $user->user_registered > 30){
   $id = ($user->ID); 
       wp_delete_user($id);
   }

   //Yearly Subscribers
   if ($user->user_plan == 'year' and date('Y-m-d H:m:s') - $user->user_registered > 365){
   $id = ($user->ID); 
       wp_delete_user($id);
   }
             }
}
add_action('wp_footer','delete_users');

Your Answer

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

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