Last active
November 29, 2022 07:42
-
-
Save benilla/a5d881df36dfd3dd9c583c79158c5365 to your computer and use it in GitHub Desktop.
Wordpress cron: Unschedule all specified single events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your
$cron
key is the timestamp, so no need to usewp_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".