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 improvefunction 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');
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
BrajeshHi 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
BrajeshHi 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
BrajeshHi Tosin,
Thank you for the patience.Please use this plugin.
https://github.com/buddydev/delete-inactive-bp-usersIt deletes any user with ‘subscriber’ role who registered 30 days ago but never logged to the site.
Regards
Brajesh
You must be logged in to reply to this topic.