What is the WP scheduler also know as WP cron? It is a set of features which enable a plugin to schedule future tasks. Example of tasks are the publishing of a post, a backup, a cache folder maintenance and so on.
There are mainly two kind of scheduling: the one shot, when a task is executed once, and the recurring where a task is repeated on a specific interval (once a day, once a week or even every five minutes).
Actually WP does not execute the scheduled task it just fire 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 to an 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 image 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 3:00 fire the “backup” hook and the reschedule for the next week
- and so on…
WP provides out of box some recurring schedules: hurly, twicedaily, daily, weekly which are used by WP for the ordinary maintenance (check for updates and so on).
WP (or better PHP) has not an internal clock, so it leverage 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 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 our there is (IMHO) WP Control (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 calling the script
It can be invoked in many different way:
- 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 autotriggering adding to your wp-config.php file:
WP has an health panel
Latest versions of WP have a Site Health panel, under Tools, which gives many useful information and warning even about the cron system. Check it, is almost unknown!