Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save champsupertramp/be017dd19009a580a0ff635f745d7e10 to your computer and use it in GitHub Desktop.
Save champsupertramp/be017dd19009a580a0ff635f745d7e10 to your computer and use it in GitHub Desktop.
Ultimate Member Cron - Delete users awaiting email confirmation after 5 days without getting confirmed
/**
* More Ultimate Member tutorials at www.champ.ninja
*/
add_action( 'um_cron_delete_users_cron', 'um_delete_users_awaiting_email' );
function um_delete_users_awaiting_email(){
$args = array(
'fields' => 'ID',
'number' => -1,
'date_query' => array(
array( 'after' => '5 days ago midnight', 'inclusive' => true ),
),
'meta_query' => array(
"relation" => "AND",
array(
"key" => "status",
"value" => "awaiting_email_confirmation",
"compare" => "="
)
)
);
$users = get_users( $args );
foreach( $users as $user ){
um_fetch_user( $user->ID );
UM()->user()->delete();
}
}
if ( ! wp_next_scheduled( 'um_cron_delete_users_cron' ) ) {
wp_schedule_event( time(), 'daily', 'um_cron_delete_users_cron' );
}
@FromEarthToday
Copy link

@champsupertrap

Wow, that was fast, must say I wasn't expecting a reply for a few days, very much appreciated. As for the fix, where's the facepalm emoji when you need it; I changed all the quotes multiple times trying to troubleshoot ... and never even noticed line 10 missing one.

Again, many thanks for the extremely fast reply and resolution. Enjoy your evening :)

Regards

@theBAY84
Copy link

Hey! What do I have to add to the code if I would like to delete all users after 1 hour without getting confirmed? Do I have to replace "5 days ago midnight" or what do I have to add to the code? Best regards!

@pbaines-git
Copy link

For anyone else looking to use this in 2024, I found I could only get the function to run by updating the following:

'um_fetch_user( $user->ID )' -> `um_fetch_user( $user )'

Looks as if $user now returns the user ID by default.

@sao321
Copy link

sao321 commented Oct 6, 2024

For anyone else looking to use this in 2024, I found I could only get the function to run by updating the following:

'um_fetch_user( $user->ID )' -> `um_fetch_user( $user )'

Looks as if $user now returns the user ID by default.

Hi, I tried your edit but it doesn't seem to work.

Do you mind to post your full snippet? Thanks!

@pbaines-git
Copy link

Hi, here's my latest version. This is untested since the latest versions of UM have been released though, but feel free to test.

`function um_delete_users_awaiting_email(){
$args = array(
'fields' => 'ID',
'number' => -1,
'date_query' => array(
array( 'after' => '5 days ago midnight', 'inclusive' => true ),
),
'meta_query' => array(
"relation" => "AND",
array(
"key" => "account_status",
"value" => "awaiting_email_confirmation",
"compare" => "="
)
)
);

$users = get_users( $args );

foreach( $users as $user ){
um_fetch_user( $user );
UM()->user()->delete();
}

}`

@sao321
Copy link

sao321 commented Oct 7, 2024

Hi, here's my latest version. This is untested since the latest versions of UM have been released though, but feel free to test.

`function um_delete_users_awaiting_email(){ $args = array( 'fields' => 'ID', 'number' => -1, 'date_query' => array( array( 'after' => '5 days ago midnight', 'inclusive' => true ), ), 'meta_query' => array( "relation" => "AND", array( "key" => "account_status", "value" => "awaiting_email_confirmation", "compare" => "=" ) ) );

$users = get_users( $args );

foreach( $users as $user ){ um_fetch_user( $user ); UM()->user()->delete(); }

}`

Thank you very much, though this does not seem to work when I tried to force run the cron job...

Not sure if the latest version made any changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment