Cron is a UNIX tool (Linux, Mac, and so on) that is used to schedule tasks and run them automatically when the time comes. It is normally used to schedule periodic tasks (such as, for example, checking for system updates, downloading emails from the server, or making backups), although it can also be used to execute one-time tasks (such as, for instance, downloading a specific file overnight).
As you can imagine, WordPress also needs to be able to run tasks periodically. A couple of classic examples of this are the publication of scheduled posts or the installation of automatic core updates. In theory, you should be able to perform those tasks using your server’s
cron tool, but there are many instances in which it may not be available and, therefore, WordPress can’t rely on it. That’s why the WordPress Core team implemented its own solution to schedule and execute tasks periodically: WP-Cron.
WP-Cron is a small feature included in WordPress core that is used to schedule and run tasks (both one-time and periodic tasks). As I said, there are several WordPress features that use WP-Cron (publication of posts, updates, etc.), but it can also be used (and abused) by your plugins.
This is how WP-Cron works: each time a visitor accesses a WordPress page, WordPress receives a request to serve a certain page. It’s at that moment when WP-Cron runs and checks if there are any scheduled tasks that should run “right now.” If there are, WordPress will run them and, once they’re done, it’ll finally serve the request.
The first danger we have with WP-Cron is due to a technical limitation. Since WP-Cron only runs when a visitor requests a certain page, it is possible that websites with low traffic don’t run tasks when they’re supposed to run. For example, if you schedule a task for next Saturday at 8 in the morning, but the first visitor comes on Saturday at 3 in the afternoon, the task would not run until 3pm… even though it was set to run at 8am! This is usually not a big deal, but you should be aware of it.
The second danger we face with WP-Cron is that scheduled tasks have a negative impact on your website’s loading times. If a visitor arrives on your website and WP-Cron must run a slow task during the request of that visitor, WordPress will take longer than usual to serve the page.
Last, but not least, the third danger we can find with WP-Cron is that the tasks will never be executed. If we have a cache system or our website is behind a CDN, it is possible that the cache itself satisfies incoming requests and, therefore, WordPress wouldn’t even run. If WordPress doesn’t run, WP-Cron doesn’t run either and pending scheduled tasks remain… well, pending.
A fantastic contact plugin using the block editor. In its simplicity lies the true power of this plugin. I love it, very versatile and it worked perfectly for me.
A Solution to WP-Cron Problems
These three problems with WP-Cron are well known to all and, luckily, there is a solution to mitigate them completely. Basically, you have to deactivate WP-Cron so that it doesn’t run when WordPress serves a request. However, since you want the tasks to be executed periodically, you have to configure an external tool (usually, your server’s
cron) to trigger a specific request to WordPress telling it to “run pending scheduled tasks now.”
Here’s how you can do it with some hosting providers:
WP Control. A Plugin to Manage Scheduled Tasks in WordPress
By default, WordPress does not offer any mechanism to view nor manage your scheduled tasks. If you want to be able to check what tasks are scheduled, when they will be run, or even force their execution manually at any given time, you’ll need a plugin.
There’s plenty of plugins that solve this issue, but the one I recommend for managing your scheduled tasks is WP Control . This plugin allows:
- View all cron events along with their arguments, recurrence, callback functions, and when they are next due.
- Edit, delete, and immediately run any cron events.
- Add new cron events.
- Bulk delete cron events.
- Add, edit, and remove custom cron schedules.
As you can see, WordPress scheduled tasks are very useful, although we must be careful and make sure they won’t slow down our website. Luckily, the hosting providers offer us mechanisms to optimize how WP-Cron runs, and there’s plenty of plugins to keep track of what tasks are scheduled and control how and when they should be executed.
Featured Image from JESHOOTS.COM on Unsplash.
Leave a Reply