BuddyDev

Search

[Resolved] Auto delete users by role and time

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #39298

    Hello Brajesh,

    Please can you take a look at this code if its safe to use for auto deleting registered users by roles and time.
    Do you see any negative down side to this kind of code
    If possible to improve

     function auto_delete_users() {
    	global $wpdb;
    	$userlevel = 0; //0 = non_active_member // please change with your desired role.
    	$deleteafter = 30; //deleter User after X days
    
    	$query = $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users LEFT JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id WHERE $wpdb->usermeta.meta_key = %s AND $wpdb->usermeta.meta_value = %d AND DATEDIFF(CURDATE(), $wpdb->users.user_registered) > %d", $wpdb->prefix.'user_level',$userlevel,$deleteafter);
    
    	if($oldUsers = $wpdb->get_results($query, ARRAY_N)){
    		foreach ($oldUsers as $user_id) {
    			wp_delete_user($user_id[0]);
    		}
    	}
    }
    
    add_action('daily_clean_database', 'auto_delete_users');
    
    wp_schedule_event(time(), 'daily', 'daily_clean_database'); 
  • Keymaster
    (BuddyDev Team)
    Posts: 24645
    Brajesh Singh on #39310

    Hi Tosin,
    Thank you for the question. There are issues with the above code.

    1. https://developer.wordpress.org/plugins/cron/scheduling-wp-cron-events/#scheduling-the-task
    The wp_schedule_event should not be called on each page load. It is a bad idea.

    2. instead of using deprecated user_level meta key, a much better option will be to work with the user_status from the users table.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #39313

    Can you please help provide a safe solution to this. I would feel much safer with your own code.

    Thank you

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #39380

    Gentle reminder sir

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24645
    Brajesh Singh on #39381

    Hi Tosin,
    Thank you for the reply. I am sorry, i missed to reply earlier. Please allow me a day. I will put it as tiny plugin( we need to convert to plugin to utilize the activation/deactivation hook for one time scheduling).

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #39414

    Reminder: Any update on this sir

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24645
    Brajesh Singh on #39430

    Hi Tosin,
    I am sorry, I haven’t been able to post the code. The reason is WordPress does not keep any information about inactive users. BuddyPtress or other plugin does.

    In order to help you, I need to know your strategy for finding inactive users.

    Are you using different roles(from other thread) to mark active/inactive users or should I consider the inactivity state used by BuddyPress?

    Thank you
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #39434

    I want to use the two conditions to identify inactive user

    1. User’s role should be (subscriber)
    2. The inactivity state used by BuddyPress ()

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24645
    Brajesh Singh on #39453

    Hi Tosin,
    Thank you for the patience.

    Please use this plugin.
    https://github.com/buddydev/delete-inactive-bp-users

    It deletes any user with ‘subscriber’ role who registered 30 days ago but never logged to the site.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #39467

    WOW!

    Thanks you so much brajesh you are simply awesome. I’ll test it out, Is it ok to leave it always active or do I need to deactivate after some time.

    Thanks

You must be logged in to reply to this topic.

This topic is: resolved