WordPress Simple Cronjobs

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 mypluginschedulecron() on each load. If your cronjob (myplugincronjob) is not registered, it‘s scheduled into WordPress’s cron and becomes a hook. Once it’s registered/schedualed with WordPress, the second action myplugincronjob is usable and runs your cronjob function (myplugincronfunction).

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.


Leave a Reply

%d bloggers like this: