Sometimes you’ll hear, at a meetup for example, this word “cron” (or is it kron? “Chron”?!) around WordPress. Today we’ll understand what the heck it is and what it helps us do. WordPress scheduled tasks are common and useful for everything from ecommerce plugins to WordPress core itself.
The power of WP Cron is real, because the power of cron-tasks in general is real. We’ll have a new article with a number of useful code samples soon. Today we’ll focus on what a cronjob is in general, and then specifically how that understanding will make WordPress cron jobs make a lot of sense to us.
Why Do People Use
cron in general?
cron in a term used in a computer programming to mean “Something at a specific time.” So any time you want to schedule some specific subroutine, WordPress function, etc to run every day at midnight, I’ll need to use a system like a WordPress cron job to do that. In WordPress, scheduled tasks are done using a system that works pretty similar to a “crontab”, but which is subtly different. But I’m getting ahead of myself.
cron word about?
In short, the
cron spelling is related to words like chronograph (time keeper) from the Greek “chronos”. But the reason it’s lacking an
h? Best I know it’s that English or computers are weird. I’m not sure which. Even the Wikipedia page on
cron doesn’t really explain where the
h went, but it does highlight that this has been a “Unix” standard for a while.
So that’s why we’re not talking about “wordpress cron job”, but “WordPress scheduled tasks.” The meaning is nearly identical, but WordPress doesn’t use the Unix cron system directly.
What’s a Crontab?
Unix, if you’re not familiar, is a direct predecessor to the modern operating systems of Linux and macOS. So something that is a “Unix standard” is well known and familiar to lots of programmers and computer people.
To those people, crontab is essentially a listing of all the tasks that a computer should run at a specific time. It’s a slightly confusing structure for the file, but it’s been working well around the world for decades. It has 6 columns,
day of month,
day of the week, each represented by a integer. Then the sixth element of a given line is the Unix CLI command to execute. All of this makes “every first Monday of the month at 3:24am” or “every five minutes” possible to state in a pretty compact format.
Did I lose you in the last bit? No worries.
cronjobs are not super important for the rest of this article.
Does WordPress WP Cron use Crontab?
Crontab isn’t super important to us, because we’re actually here to understand the WordPress scheduled tasks system. So what is wp-cron?
In short, it’s a system (like Crontab) that allows for specific tasks (or “cronjobs”) to be done at a specific future time or on a specific schedule. It’s not quite the same as Crontab; WordPress doesn’t directly use that. Why not use
Mostly because it would make the installation of a WordPress site more difficult, and require more system admin expertise than most WordPress users have. So WordPress came up with system that was much easier to get running if you’re new. But, then what does wp-cron do?
How Does a WordPress Scheduled Task Work?
In short, to avoid additional configuration complexity a WordPress scheduled task gets executed at the end of serving a webpage to a visitor.
WP-Cron is the usual name we call the subsystem of WordPress that maybe runs a specific function or system after a normal page load. So your site will typically use this to do some other WordPress administration task that has been scheduled to run, perhaps like seeing if there’s a WordPress code update to download and install.
WordPress cron settings don’t really exist in this system. Instead the (core) WordPress software just checks when plugins (or internal WordPress core subsystems) want to run, and then call them to run when they wanted to be called.
Conceptual Working of WP Scheduled Tasks
So, that’s how WordPress cron tasks work. They’re using something called the WP Cron system, but the cron needs the WordPress site to be getting called to show new pages to work.
This is an important point, so I’ll say it again: if your WordPress cron jobs aren’t working, it may be because your WordPress site isn’t getting enough visits at the right times of days for them to work like you’d expect. Because without visits onto which your WordPress site can tack in its scheduled cron tasks, your WordPress cron tasks won’t run on time, or at all.
Downsides of WP Cron Event: They fail
The big problem with the WP-Cron system, as I suggested above is when you’ve got a true hobby website built with WordPress. A website that you and just a few friends look at from time to time, your scheduled tasks won’t run. Also, if you have a complex caching front end for your WordPress site, which makes your site not re-render pages much, it’s common to feel that your WordPress scheduled tasks are broken.
Sometimes people will offer you the advice that you should “replace WordPress cron with a real cron job.” They’re not meaning that you should break the WP cron system. Instead, the advice is that you force WordPress to run its cron tasks on a regular schedule. To make your Unix server’s
crontab call into WordPress and run any tasks it has scheduled on some regular and computer-controlled interval. (At the end of the upsides section, we have the link that’ll explain how to configure
crontab to solve your scheduled tasks not firing problem.)
Upsides of WordPress Scheduling Tasks System
So people get frustrated that a WordPress cron job doesn’t work like a normal cron job. With a normal cronjob, if the computer’s on, your registered command will run. No exceptions. A WordPress Cron Job requires something more like the understanding that it’ll run “about the correct time, on a normal website with average levels of traffic.”
But that, then, is the beauty of the WP-Cron system in WordPress. Even on the site your old Uncle Joe set up on Hostgator (or whatever) 5 years ago, scheduled tasks are possible. Neither Uncle Joe, nor Hostgator, has do anything special to allow Joe’s site to run cronjobs. Instead his site has been able to run backups, send daily-digest emails, and a whole lot more without any special programming whatsoever. That Uncle Joe can change backup plugins without rewriting (and relearning how to edit) his
crontab file is precisely the beauty and power of the WordPress event scheduling system.
BTW, if you are technical and want to solve the problem mentioned above about the fact that WP-Cron isn’t a real cron job, there’s a great explanation in the WP.org Developer Handbook: Hooking WP-Cron Into the System Task Scheduler. It’ll help you solve that WP-Cron tasks not running problem for good.
That’s What WordPress Cron Jobs Look Like
We’ll cover the WordPress cron job example code and related questions for next time. What I hope I’ve given you here is the organizational understanding of WordPress Cron Job pluses and minuses. WP Cron has some real downsides, but it’s also really useful in a variety of circumstances. Keep an eye out for a more specific WordPress cron job tutorial soon