WordPress makes it super simple to run cronjobs.. To add one, just place the following code in your
functions.php file and change the function/action prefix’s to your relevant plugin name/functions.
A little break down for those unfamiliar: the first action runs
myplugin_schedule_cron() on each load. If your cronjob (
myplugin_cronjob) is not registered, it’s scheduled into WordPress’s cron and becomes a hook. Once it’s registered/schedualed with WordPress, the second action
myplugin_cronjob is usable and runs your cronjob function (
If you want your cronjob to occur less frequent than ‘
daily’, you need to add a additional timespan. Since what I’m writting this for has dynamic timespans, I’ve added several intervals for weekly, monthly, quaterly, biyearly, and yearly cronjobs:
If you’re uncertain if your cron function is working and want to test it, you can uncomment the third action and refresh the page (to run what WordPress cronjob will run on it’s intervals). If you’re uncertain if your cronjob is actually scheduled you can install Cron View, a plugin that displays the available schedules and the scheduled tasks.