This article will go into great detail about how WordPress scheduled events work and why they are different than Cron Jobs.
Essentially WordPress Scheduled Events are DEPENDENT on site visits in order to execute while Cron jobs are not.
Before I get into the details with WordPress Scheduled Events I want to go over Cron jobs and exactly what they are.
What are Cron Jobs?
Cron jobs are a function of the Unix operating system that run a specific script at a specific time. These scripts are run in the background and are usually automated by a job scheduler, which is like a program that executes other programs at certain times. These Cron jobs are great for administrative tasks like email handling and other operations at the server level. They work by constantly checking if their scheduled time to execute matches the current time; if they match then the Cron job is executed, if not then nothing happens.
Since all logic is run within the server operating system Cron jobs are executed exactly at the moment they were scheduled for.
For example, if a Cron job is scheduled to run at 9:09:31 PM on August 3 then it will be executed at that exact time, there is no delay.
What are WordPress Scheduled Events?
There are many parts of WordPress that need to run automated tasks at set intervals, like the plugin update checker. These tasks cannot be scheduled as pure server-level Cron jobs because WordPress does not have access to that type of server-level operating system privileges.
So, since true Cron jobs are not available, WordPress created its own way of running scheduled tasks in the background – WordPress scheduled events.
How Do WordPress Scheduled Events Run?
Although it is not possible to schedule a Cron job, WordPress can create an automated task that acts like one.
WordPress has a lot of great tools in its arsenal that it uses to mimic Cron jobs.
- Action Hooks
- Filters
- Transients
- HTTP Requests
Lets walk through exactly how WordPress creates these automated tasks.
- WordPress adds an action to the init action hook to run the
wp_cron
function. This function is essentially run on every page load so it must be extremely efficient. - The wp_cron function runs a few sanity checks before retrieving all know scheduled events from the cron option.
- Once all of the scheduled events are retrieved, wp_cron calls the
spawn_cron
function on each event. - The spawn_cron function does some more sanity checks before executing the respective event function via the
wp_remote_post
function.
This all happens behind the scenes and WordPress gives developers access to this functionality through the wp_schedule_event
function.
One thing to note before we move on is that this function is dependent on site visits.
This means that if your site is never viewed, the init action hook is never fired, and these scheduled events will never run. So, if you schedule a post to publish at 3:26, but no one visits your site until 5:15, the post does not get published until 5:15.
What are the differences?
Now that we completely understand what Cron jobs are and what WordPress scheduled events are we can compare them and determine their differences. At a very low-level, these are completely different in almost every way – from management to execution- but at a high-level there is only one major difference between them: guaranteed execution time.
As I stated before, WordPress scheduled events are dependent on site visits while Cron jobs completely independent from site visits. This means that if you have a WordPress scheduled event set to run at 9:09:31 PM on August 3 then it will not run until you get a site visit at or after this time. So if the next site visit occurs at 10:22:11 PM on August 3 then that is when the scheduled event will execute. Cron jobs on the other hand will execute exactly at their designate interval.
Overall, WordPress scheduled events cannot be trusted to run exactly at a set interval since they are dependent on site visits.
When to use WordPress Scheduled Events
WordPress scheduled events aren’t 100% reliable, however I want to make it clear that these are great tools that every developer should know how and when to use.
Consider WordPress core, it creates a few default scheduled events to check for updates and clean up the database. These functions do not need to run at an exact moment, but can be run during a lenient time period, which makes them perfect candidates for scheduled events. You can have a look at all of your WordPress scheduled events by using this great plugin, WP Control.
The best functions to use with WordPress scheduled events would be any function that needs to be run at a certain interval, but does not need to be run at an exact moment in time.
Another great example of a WordPress scheduled event (that we created and use a lot) is one that deletes all expired transients. WordPress will not automatically delete expired transients, so we created a scheduled event to run once a day to check for and delete expired transients.
Have you used WordPress scheduled events in the past? Let us know whether they worked the way you wanted them to or not in the comments below!
2 Responses
Hi Kyle,
You can make wp-cron run as an actual cron job to overcome the timing issue. tuts+ talks about it here: http://code.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress–wp-23119
Look for the section labeled “Possible Solution to Most WP-Cron Problems”. Steps 1 and 2 would change it.
Yes you are 100% correct, but article is focused on describing how WordPress scheduled events work. Also, that method described in tuts+, which by the way is a fantastic article, references the ability to access your cPanel. For this scenario we are trying to show a developer what is able to be done only using WordPress scheduled events when you don’t have that type of hosting access.