Recently we’ve been playing around with the wordpress cron features. The one thing that’s very badly documented is how to know when its working and how to cancel them.
When scheduling the cron jobs via wordpress, the documentation explains that you should set them up using the “time()” function as the first argument in wp_schedule_event($timestamp, $recurrence, $hook, $args);.
Only when trying to un schedule an event do we find out that this is a very bad idea. Bad idea since you probably don’t know what that “time()” function actually spit out. Why is that important you ask, well its needed for that wonderful new function:
[php]
wp_unschedule_event($timestamp, ‘my_schedule_hook’, original_args );
[/php]
The first argument there again, is the exact timestamp you generated however long ago.
OPTIONS
So what are your options at this point? Where are these things being stored anyway? My guess ironically enough is in the wordpress options table. Now since that’s not a realistic place to search (for the layman), lets try this, what’s actually in that wp_unshceulde_event() function? Aha, in that function we see how this thing floats around in memory like so many other things in the CMS. So lets borrow a line from the function and use the following. Then stand back:
[php]
$crons = _get_cron_array();
var_dump($crons); // or print_r
[/php]
You should get a result like the following
[php]
[1326255415]=>
array(1) {
["my_twicedaily_event"]=>
array(1) {
["40cd750bba9870f18aada2478b24840a"]=>
array(3) {
["schedule"]=>
string(6) "twicedaily"
["args"]=> array(0) {}
["interval"]=>int(43200)
}
}
}
[/php]
Now we have all cron jobs spilled across your home page. Did i mention you should make sure to use that command in an unimportant area? Well lets hurry up since this code is messing up your wonderful site where ever it is. Note the time stamp of the event you would like to remove. Thats what you will use for the first argument in wp_unschedule_event().HOOK LINE AND SINKER
CONCLUSION
Much like how you launched the feature, the wordpress documentation doesnt lie. I.E. there is no return value yippy. You will simply need to recheck that variable or wait and monitor the cron ran event. I personally went for the first, all brute force and everything.
[php]
wp_unschedule_event("1326255411", ‘my_twicedaily_event’);
wp_unschedule_event("1326255415", ‘my_twicedaily_event’ );
$crons = _get_cron_array();
var_dump($crons);
[/php]
Yeah it started so quitely, i launched multiple instances of the same thing. Judging from the stamp, right after each other hmm.
$crons
= _get_cron_array();
- var_dump or print_r, whatever your flavor var_dump($crons);
Debe obtener un resultado similar al siguiente
[1326255415]=> | |
array(1) { | |
[“my_twicedaily_event”]=> | |
array(1) { | |
[“40cd750bba9870f18aada2478b24840a”]=> | |
array(3) { | |
[“schedule”]=> | |
string(6) “twicedaily” | |
[“args”]=> | |
array(0) { | |
} | |
[“interval”]=> | |
int(43200) | |
} | |
} | |
} |
- wp_unschedule_event(“1326255411”, ‘my_twicedaily_event’);
- wp_unschedule_event(“1326255415”, ‘my_twicedaily_event’ );
- $crons = _get_cron_array();
- var_dump($crons);