This is kind of a stupid question…
I scheduled a action to run every hour:
if(!wp_next_scheduled('my_hourly_events'))
wp_schedule_event(time(), 'hourly', 'my_hourly_events');
add_action('my_hourly_events', 'the_function_to_run');
function the_function_to_run(){
echo 'it works!';
}
How can I test if this works without waiting an hour? 🙂
I tried adding wp_clear_scheduled_hook('my_hourly_events');
before this code and adding wp_cron()
after, but I don’t see my function running…
edit:
ok, I added a trigger_error()
inside my function, checked out the apache error log, and it’s there 🙂
So now I’m even more confused:
-
How can the wp-cron run in the background? because apparently that’s what happens if I see no output…
-
this doesn’t seem to work in a object context; why?
My favorite plugin for that is Core Control which has very nice module for display of what is going in the cron – which events are set up, when are they next firing, etc.
On getting your hands dirty level see
_get_cron_array()
, which returns internal stored data for cron events (top level of keys are timestamps).wp-cli is another way:
Listing Events
Schedule an Event
Then you can visit the front-end of the site and refresh a couple times to make sure it’s triggered. Then run
list
again to see that it’s no longer scheduled.Run Directly in Terminal
One quirk to be aware of is that wp-cli runs in the wp-admin context, but WP Cron runs on the front end. That usually isn’t a problem, but when you’re writing jobs, make sure that you
require()
anywp-admin/includes
files that your code expects to be loaded.If you don’t, then the job will work fine in wp-cli, but will produce a fatal error when running normally.
Other commands
Run
wp help cron
for more details.