Skip to content

Instantly share code, notes, and snippets.

@benilla
Last active November 29, 2022 07:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benilla/a5d881df36dfd3dd9c583c79158c5365 to your computer and use it in GitHub Desktop.
Save benilla/a5d881df36dfd3dd9c583c79158c5365 to your computer and use it in GitHub Desktop.
Wordpress cron: Unschedule all specified single events

Wordpress cron: Unschedule all specified single events

<?php
$crons = get_option( 'cron' ); //Undocumented WP function which returns an array of all crons
$cronHook = 'hook'; //You will need to know the hook for the scheduled cron
$needle = 'first.last@example.com'; //The val which you search for in the crons you want to unschedule. In this case, I wanted to unschedule all crons to this email.
foreach ($crons as $cron => $cronAction) {
if( array_key_exists($cronHook, $cronAction) ){
$firstKey = reset( $cronAction[$cronHook] ); //This key is a randomized ID assigned by WP when the cron was scheduled. Contains the args.
$cronArgs = $firstKey['args'];
if( in_array($needle, $cronArgs)){
$timestamp = wp_next_scheduled( $cronHook, $cronArgs );
wp_unschedule_event( $timestamp, $cronHook, $cronArgs );
}
}
}
@BrianHenryIE
Copy link

BrianHenryIE commented Aug 7, 2018

Your $cron key is the timestamp, so no need to use wp_next_scheduled to get it again (untested!). Thanks for the Gist.

Also, needs is_array($cronAction) && on line 7 because WordPress has a "version" key whose value, for me, is just "2".

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