Disabling the WordPress cron can improve the performance of your website, but you need to replace it with something else, otherwise, WordPress, and any plugins you have installed, will not be able to run any scheduled tasks.
What does the WordPress cron do?
WordPress has a range of automated tasks that is needs to perform on a regular basis, such as checking for updates. However, WordPress doesn’t have the ability to wait until a set time and run tasks by itself. Instead, WordPress checks for overdue tasks every time someone visits the site. If WordPress finds a task that should have been done since the last time someone visited the site, it runs it.
Why would I want to disable the WordPress cron?
There are two main drawbacks to the WordPress cron:
It is dependent on people visiting your website
The WordPress cron needs a steady stream of website visitors to work well. When there are periods when your website doesn’t get any traffic, such as during the night, any scheduled tasks will be left to go overdue.
For some tasks, this might not matter too much, but you may have tasks that you specifically need to run during quiet periods, such as backups.
It eats up system resources
WordPress needs to allocate server resources to do anything. Every time someone visits your site, WordPress will use your host’s CPU power and RAM to construct the relevant webpage and send it to the person’s browser. Because the WordPress cron is triggered every time someone visits the site, it means that every visit uses up a little more resources than it otherwise would.
On a basic site with low levels of traffic, this won’t be a huge issue, but as your site’s traffic grows, and you start adding plugins, this extra resource use can become significant. In some cases, it could even lead to increased hosting costs as you require more resources to handle the load.
How do I run scheduled tasks without WordPress cron?
The scheduled tasks within WordPress are important, so we need to make sure they are still being run. What we want is an alternative way to trigger them on a regular basis that is independant of people visiting our site. WordPress triggers scheduled tasks by running a specific file. This file then checks what scheduled tasks need to be run and runs them. What we need is a command that will run this same file, and a way of scheduling that command to run at regular intervals.
How to run commands at regular intervals
Most hosting platforms will come with a method for running commands on a schedule. The interface for the task scheduler may look slightly different from one hosting company to another, but they should all work in a similar way. You should have the ability to select the minutes, hours, days, months, and years that a task should be run.
The aim here is to pick a shedule that ensures tasks are not left too long, but not so frequent that it needlessly uses up server resources. I would normally recommend anything from every five minutes to once or twice an hour. However, feel free to adjust this for your own needs.
The command to run scheduled tasks
WordPress is written in a language called PHP, and the file we want to run is a .php file. Fortunately there are several ways for us to run this php file from the server, the most popular of which is wget.
Wget is a command that is used to make requests using the same internet protocols as a web browser. Normally it is used to download files, but we can also use it to run php files. Here is an example of a wget request to the php file that runs out scheduled tasks on a linux based server:
/usr/bin/wget -q -O /dev/null https://www.yourwebsite.com/wp-cron.php?doing_wp_cron
Without going into too much detail, the “/usr/bin/wget” runs the wget command. The “-q -O” are options that are passed to wget and the “/dev/null” is a way to tell wget that we don’t need to store anything from the request. The “https://www.yourwebsite.com/wp-cron.php” is where we tell wget where the .php file we want to run is located. Notice that we are giving wget a website URL and not a file path. In this example I have used an dummy domain name, but on yours you would use the domain name of your website. The “?doing_wp_cron” is an option we pass to the .php file.
If you find this does not work, you may need to contact your hosting company to ask them what the most apporpriate command is.
How to disable the WordPress cron
Now we are running the scheduled tasks at regular intervals, we need to tell WordPress to stop trying to run them every time someone visits the site. To do this we need to edit the wp-config.php file.
When someone visits our WordPress site, before WordPress runs the scheduled tasks file, it checks for something called a php constant. WordPress uses constants for all sorts of things, but in this case it is looking for a specific constant that will tell it if it should run the scheduled task file. If the constant doesn’t exist, it will run the file as normal. If the constant does exist, WordPress will skip running the scheduled tasks file.
As we have set up a better way to run scheduled tasks, we don’t want WordPress trying to run them as well, so we need to add this constant to the wp-config.php file where WordPress can find it.
Editing the wp-config.php file
The wp-config.php file should be in the main folder where you installed WordPress. Different hosting companies might name their folders differently, but normally there is something obvious like a folder called “public” or “www”. If you can’t find it, or are unsure you are in the right place, ask your hosting company.
Before we make any changes, I recommend taking a copy of the wp-config.php file. We are only making a simple edit, so you shouldn’t hit any issues, but it’s always worth making sure you can revert back just to be sure.
In the wp-config.php file, you should see a line that says:
/* That's all, stop editing! Happy publishing. */
We want to add a new line of code just above that line.
To define the php constant that tells WordPress not to run the WordPress cron every time someone visits the site, we need to add the following line of code:
Once you save the file, WordPress should pick up that constant and skip running the WordPress cron when someone visits the site.
How do I know it is running correctly?
A really useful plugin for showing you information about your scheduled tasks in WordPress is called WP Crontrol. If you install and activate this plugin, then navigate to the “Cron Events” page (under the Tools menu in the WordPress dashboard), it should list out all the scheduled tasks, along with when they are next due to run.
Leave your site longer than the interval you set the cron to run (I’d recommend leaving it to run a few times just to be sure), then check the Cron Events again and see if any have gone overdue. If there are no overdue tasks, it means the new setup is working correctly.
If you do see some overdue tasks in the Cron Events list, you may need to contact your hosting company to help diagnose the problem.