Auto delete Wordpress users according to time

On my WordPress site I require registration with email confirmation to help filter out spam users. How do I delete users that haven't activated their accounts by email automatically?

Is it possible to automatically delete users that are X days old? For example: if the user is 7 days old and not active, then the account should be automatically deleted. I'd like to check for this every day. Could someone please explain how to do this?

I am using the field name tecla_users in my database.

Topic php spam users customization plugins Wordpress

Category Web


You can include this code by creating a new custom plugin which help you to stop this when you deactivate the plugin.

wp_schedule_event(time(), 'daily', 'my_dailyClearOut');

function my_clearOldUsers() {
    global $wpdb;

    $query = $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE datediff(now(), user_registered) > 7");

    if ($oldUsers = $wpdb->get_results($query, ARRAY_N)) {
        foreach ($oldUsers as $user_id) {
            wp_delete_user($user_id[0]);
        }
    }
}

add_action('my_dailyClearOut', 'my_clearOldUsers');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.