Scheduling the publication of posts in WordPress, checking plugins for updates, creating backups - these are all typical examples of tasks that take place repeatedly. And this is where WP-Cron comes into play in WordPress. In this article, we explain what this means and when server-side cron jobs make more sense.
As you know, your computer is constantly performing countless calculations with ones and zeros at breakneck speed, all of which have a job to do. Many of these are tasks that need to be repeated. Over and over again - like brushing your teeth. That's why I have the following question for you:
If you could automate brushing your teeth, would you do it?
Your WordPress website has automated maintenance tasks that it needs to perform on a regular basis. In this article, we'll look at what these tasks are, what problems they can cause and how Raidboxes makes it super easy for you to manage them.
Every time your local machine shows you a pop-up message that an update has been found or an email has arrived in your inbox, it's most likely a cronjob working behind the scenes.
"*" indicates required fields
What is Cron?
Cron is a task scheduling command that is integrated in Linux-based systems - such as Ubuntu, Debian or Mac OSX - and also on servers. Under Windows, the functionality is called "Task Scheduler".
Documentation on the use of the cron function in these systems can be found here:
- Windows - Task Scheduler (uses a Graphic User Interface [GUI] for more user-friendliness)
- Mac - Launched Jobs and Cron Jobs (uses Terminal and requires some knowledge of bash scripting)
- Server - Example: cPanel - (uses either an integrated GUI or terminal)
What does Cron do anyway?
The purpose of Cron is to perform repetitive tasks - for example, checking for available updates or sending and receiving emails. This is normally set up for you by default. But the more software you have installed on your computer, the more jobs will most likely be added. For example, the screenshot below shows a cron job in the task scheduler in Windows 10.
Wondering how that works? Cron works by scheduling tasks based on the time of your device and can be set up by minute, hour, day, week, month and year. In Cron, you define a specific time at which an action should be executed.
What is WP-Cron?
WordPress Cron, also known as WP-Cron, is a PHP file that can usually be found in the root directory of your WordPress installation.
And what does WP-Cron do?
The WordPress cron file (wp-cron.php) contains the automated tasks that your WordPress installation needs to perform. For example, things like checking for updates to plugins and WordPress or publishing scheduled posts. For more information, see the documentation "What is WP-Cron" in the official WordPress Plugin Handbook.
WP-Cron versus Cron
The main difference between WordPress Cron and the Cron scheduler is that wp-cron.php is only executed when a user visits your website and triggers a page load. This means that WordPress requires an HTTP/HTTPS request from your website to execute scheduled tasks. The cron task is then placed in a queue to be executed.
You may rightly be wondering:
Why is WP Cron only executed when a user visits my website?
Well, many WordPress hosts only offer shared hosting, which usually means that the host does not give you access to the cron command. If this were the case, you would have access to the server commands and all data on the server would potentially be at risk. WordPress Cron is therefore the workaround for this problem.
The second difference is that when using Cron, you can set specific times (for example, 17:00 daily to run a task). In WP-Cron, on the other hand, you define intervals (for example 14:00 & interval X thereafter). WP-Cron then executes the task at these intervals as long as a user has visited the site. For more information, see also: Understanding WP-Cron Scheduling.
WP-Cron and performance
Due to the fact that WP-Cron is always triggered when a user visits your website, this can cause problems for both small and large websites. I'll explain why now.
Small & geographically specific websites
For smaller websites with little traffic, the problem is that the cron task is not executed often enough. An example: What if your website is supposed to create a backup at 2:00 am, but you have a small travel guide site that only operates in one city. In this case, it is unlikely that your target group will visit your website at 2:00 am and trigger the cronjob. And if it's in the off-season, you might not get any visitors to your site for days.
Larger websites & global reach
However, for websites with a large number of users, WP-Cron can cause some performance issues and even leave your site vulnerable to a DDoS attack. Now, if the mention of a DDoS attack probably just made you reach for your mouse or coffee cup in panic: don't worry too much, because there's a solution for everything.
What causes the problem?
The amount of traffic is causing the problem. For large websites, traffic is of course a must and important for business, so limiting the number of hits is not an option to solve the problem.
Remember: Every page visit leads to WP-Cron being triggered - and this naturally puts a strain on your server because it requires resources.
Is there a better way?
Fortunately, the answer is yes. While we can't simply curb traffic, open physical stores around the globe or change the way WordPress is set up. But we can change how WP-Cron is executed.
Instead of running wp-cron.php based on page visits, we can set up a cron task on the hosting server that runs WP-Cron at specific intervals. I'll show you how to do this now.
Configure the server-side execution of wp-cron.php with Raidboxes
To make your job as easy as possible, the team here at Raidboxes have built this feature directly into the RB dashboard. You literally need five clicks to set up the server-side execution of WordPress cronjobs! If you're a developer looking to save time, this is one of the easiest tweaks to make.
To activate server-side cronjobs in your Raidboxes dashboard, first go to the settings of your BOX and then click on "Cronjobs" in the sidebar.
You will now see all the setting options for your cronjobs.
Now you should make the following configuration in the cronjobs:
- Switch off "Run WP-Cron" (this prevents WP-Cron from being triggered by website visitors).
- Enable "Run WP-Cron server-side" (this adds a cron task to the server to trigger wp-cron.php at the interval you specify).
That's all there is to it!
Attention
It is therefore not necessary to contact your host, go to the cPanel or manually edit your wp-cron.php file for this customization. With Raidboxes, you only need a few clicks and can monitor and manage your cronjobs directly in the RB dashboard. This means you don't need to install an additional plugin to display your cronjobs.
If you want to make further configurations, you can also specify the interval at which the wp-cron.php should be executed in the Raidboxes dashboard.
There is a lot of speculation about the "best" interval for your cronjobs. We generally recommend not going below 5 minutes.
Edit your WordPress cronjobs
Without plugins
If you don't want to install a plugin to edit your wp-cron.php file, that's no problem! At Raidboxes you can easily manage your cronjobs via the RB dashboard as described above. Of course, you can also access your cronjobs via an SSH connection. Once you have connected to your account, you can edit wp-cron.php in the root directory of your WordPress website.
If you want to know how you can make your workflow even more efficient with SSH, take a look at our article "The perfect WordPress development workflow with Git & SSH".
With plugins
The currently most popular plugin for managing WP cronjobs is "WP Crontrol". You can also search for and install the plugin directly in your WordPress under Plugins.
On WP Crontrol's plugin page on WordPress.org you will also find helpful documentation on how to use the plugin correctly.
Manual setup of the server to trigger WP-Cron (not necessary with Raidboxes)
With a little setup effort, you can also configure your server yourself so that the wp-cron.php is executed at certain intervals.
- Edit the wp-cron.php
In wp-cron.php
- Deactivate your wp-cron.php file by adding the following code to the file:
define('DISABLE_WP_CRON', 'true');
After that, wp-cron.php will no longer be triggered when a user visits your website.
- Add a cronjob to your server
In your server environment
Depending on your hosting environment, one of the two options listed below may apply to your specific situation:
- If you're using a shared WordPress hosting service, contact your hosting provider and ask them to add a server-side cron trigger for wp-cron.php if you don't have access to the Linux commands yourself.
- Or, if you run your website with cPanel and have access to your command line, have a look at the corresponding cPanel documentation.
The WP cron file is easily overlooked when setting up a WordPress website, but it can lead to noticeable errors if your site gains a lot of traffic.
Preparing WordPress for traffic peaks
Conclusion
I hope this article helps you to understand what the WordPress cron function is and how it works. Used correctly, it can help you to reliably automate important tasks so that you can concentrate on the essentials.
Our goal is to make the management of your WP cronjobs as easy and convenient as possible via the Raidboxes dashboard. This quick and direct access allows you to further optimize the development and management of your site.
Do you have any further questions about WP-Cron or our dashboard? Then please leave us a comment! Want to be informed about new posts about WordPress? Then follow us on Twitter, Facebook or via our newsletter.
Is the DISABLE_WP_CRON entry really - as described in the article - to be inserted in wp-cron.php? I would have rather assumed such a define entry in wp-config.php.
Best regards Stefan