What is the WP scheduler also known as WP cron? It is a set of features that enable a plugin to schedule future tasks. Example of tasks is the publishing of a post, a backup, cache folder maintenance, and so on.
There are mainly two kinds of schedule: the one shot, when a task is executed once, and the recurring where a task is repeated at a specific interval (once a day, once a week, or even every five minutes).
Actually, WP does not execute the scheduled task it just fires an event at the scheduled time so the plugin which registered that event (or the theme or even the WP core itself) can do something.
In human readable words and using an example:
- we ask WP to trigger an event every day at 8:00 which is associated with a hook, named for example “mybackup”
- we listen to that hook attaching to it a function
- at 8:00 the cron system trigger the event, fires the “mybackup” hook and WP executes all the function attached to it (yes it is all managed by WP!)
Internally WP manages a stream of events (rescheduling automatically the ones which are recurring) and at the right time (almost) it fires the attached hook executing all the attached functions.
We can imagine all that as:
- at 8:40 fire the hook “newsletter” and then reschedule the event after 10 minutes (hence at 8:50)
- at 12:35 fire the “cache-clean” hook just once
- at 14:56 fire the “publish” hook (with attached post ID argument) just once
- on Sunday at 3:00 fire the “backup” hook and reschedule for the next week
- and so on…
WP provides out of the box some recurring schedules: hourly, twice daily, daily, and weekly which are used by WP for ordinary maintenance (check for updates and so on).
WP (or better PHP) has not an internal clock, so it leverages the site traffic to activate from time to time the cron system to check if there are events to be managed and hooks to be fired.
This is one of the main problems when we need the precision of event management since low traffic sites or aggressive caching block the cron system activation and events can be managed with sensible delay.
Inspect the Cron System
To keep an eye on the cron system, the best plugin out there is (IMHO) WP Crontrol (but there are many others). If you’re curious install it and see how many planned task has your blog. You will be surprised (and many can sound pretty obscure…).
When the cron system is not working properly, and it happens enough often, it can be triggered externally by calling the script
It can be invoked in many different ways:
- as a php command configured in the server cron system (for example every hour):
- or simulating a web call with
wget(always scheduling it in the server cron system)
- or using an external service like cron-job.org
When you use an external trigger, it’s recommended to disable the WP auto triggering by adding to your wp-config.php file:
WP has a health panel
The latest versions of WP have a Site Health panel, under Tools, which gives much useful information and warning even about the cron system. Check it, is almost unknown!