• Resolved pistolero

    (@pistolero85)


    Hello. Some time ago I asked for a plugin to delete automatically inactive users after certain period of time. I was given a code snippet which worked well. The snippet from this thread.

    https://www.ads-software.com/support/topic/automatically-delete-inactive-users-cron/

    But now it stopped working, I suppose after update to version 2.8 of Ultimate member. Can you please help me to make it work again? Thanks

    Old code snippet:

    /**
     * Delete inactive users after 60 days of inactivity based on last login time.
     */
    if ( ! wp_next_scheduled( 'um_cron_delete_users' ) ) {
    	wp_schedule_event( time(), 'daily', 'um_cron_delete_users' );
    }
    add_action( 'um_cron_delete_users', 'um_cron_delete_inactive_users_by_last_login' );
    function um_cron_delete_inactive_users_by_last_login() {
    
    	$args = array(
    		'fields'     => 'ids',
    		'number'     => -1,
    		'meta_query' => array(
    			'relation' => 'AND',
    			array(
    				'key'     => '_um_last_login',
    				'compare' => 'BETWEEN',
    				'value'   => array(
    					strtotime( '10 years ago' ),
    					strtotime( '60 days ago' ),
    				),
    			),
    		),
    	);
    
    	$users = get_users( $args );
    
    	foreach ( $users as $user_id ) {
    		um_fetch_user( $user_id );
    		UM()->user()->delete( false );
    	}
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support andrewshu

    (@andrewshu)

    Hello @

    Try to use this code:

    /**
     * Delete inactive users after 60 days of inactivity based on last login time.
     */
    if ( ! wp_next_scheduled( 'um_cron_delete_users' ) ) {
    	wp_schedule_event( time(), 'daily', 'um_cron_delete_users' );
    }
    add_action( 'um_cron_delete_users', 'um_cron_delete_inactive_users_by_last_login' );
    function um_cron_delete_inactive_users_by_last_login() {
    
    	$args = array(
    		'fields'     => 'ids',
    		'number'     => -1,
    		'meta_query' => array(
    			'relation' => 'AND',
    			array(
    				'key'     => '_um_last_login',
    				'compare' => 'BETWEEN',
    				'value'   => array(
                    date( 'Y-m-d H:i:s', strtotime( '10 years ago' ) ),
                    date( 'Y-m-d H:i:s', strtotime( '60 days ago' ) ),
                ),
    			),
    		),
    	);
    
    	$users = get_users( $args );
    
    	foreach ( $users as $user_id ) {
    		um_fetch_user( $user_id );
    		UM()->user()->delete( false );
    	}
    }

    Regards.

    Thread Starter pistolero

    (@pistolero85)

    Hi. Thank you for reply. I’ll test it today and let you know.

    Thread Starter pistolero

    (@pistolero85)

    It’s working now. Thank you very much!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Delete inactive users (cronjob)’ is closed to new replies.