Automatically delete Wordpress users depending on the time since registration?

In the basic setup of Wordpress 3.1 using User Access Manager, can you automatically delete users who are less than x days old?

I did not find plugins for this function. How could this be realized? Will I be able to configure the cron job with sql or php query, when users, for example, 3 days, are automatically deleted from the database once a day? If so, can someone explain how?

Any help would be greatly appreciated - thanks in advance.

+4
source share
3 answers

user_registered wp_users. WordPress, , MySQL - DATEDIFF() SQL, , .

SQL , 30 ( ):

DELETE FROM `wp_users` 
WHERE datediff(now(), `user_registered`) >= 30

DELETE FROM SELECT * FROM , , , , .

cronjob, , PHP script, SQL . , crontab:

0 0 * * * php ~/delete_expired_users.php

cronjobs, php ~/delete_expired_users.php ( *) 0, 0 (.. ). , .

+7

Wordpress, .

-, wp_delete_user() - , user_meta , .

- wp_schedule_event() - wp-cron. , crontab ( ).

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) > 30");

    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');

( , ).

, "wp-cron" crontab - , , , cron. , .

@Sam datediff() - !

+1

Remember that wp_schedule_event () will be triggered every time the page loads, which could jeopardize your site. It’s better to check if we have a planned event and just add it.

if( !wp_next_scheduled( 'my_dailyClearOut' ) ) {  
wp_schedule_event( time(), 'daily', 'my_dailyClearOut' );  
}  
+1
source

All Articles