WP-cron is a WordPress tool that handles time-sensitive tasks to ensure your website keeps running smoothly.
A good example of a task is when you want to schedule a post to publish in the near future. WP-cron tries to ensure that your post will be published at the date and time specified.
I’ve written an article explaining how WordPress wp-cron works and where you can get more information about this topic.
Why Should You Disable wp-cron?
In some cases, the option may be to disable wp-cron and instead use cron.
Cron is a utility present in Unix and Linux systems that allows a user to schedule a script or command to be executed automatically at scheduled intervals.
By default, WordPress is set up to call wp-cron every time someone visits one of your website pages. At that moment, it checks to see if there are scheduled tasks. If yes, then wp-cron runs and executes those tasks.
Reasons to Disable wp-cron
On low-traffic websites, running wp-cron every time someone visits your website may be a problem. If the website receives very few visitors during the day, wp-cron will run tasks much less than it should.
With wp-cron running very few times a day, scheduled tasks like publishing new posts at the time specified will be delayed till someone visits your site.
Running wp-cron every time someone visits your website may also be a problem on high-traffic websites. If the website receives many visitors during the day, wp-cron will run tasks too many times, leading to resource usage problems on the server.
It will impact the performance of your website, making it load slower, for example.
As you can see, there are reasons why owners of low-traffic websites and high-traffic websites may choose to disable wp-cron in WordPress.
I’ll explain how to disable wp-cron and set up a manual cron job to replace wp-cron in two steps:
- You will disable the built-in option so that WordPress doesn’t run wp-cron.
- You must set up a manual cron job that will run at a specified time, no matter how many visitors your website has.
How to Disable wp-cron
First, we need to disable the script ( wp-cron.php ) that executes every time someone visits one page of your website.
WordPress is written in PHP, a scripting language geared towards web development. The file we need to update is a .php file.
WordPress will check for something called a php constant when someone visits your site. A constant is an identifier (name) for a simple value, and in this case, it’s looking for a specific constant ( DISABLE_WP_CRON ). If that constant doesn’t exist in the .php file, it will run wp-cron. If the constant exists and its value is true, then WordPress will not execute wp-cron.
To add that PHP constant to WordPress, you are going to edit the file wp-config.php located in your main WordPress folder.
To access the wp-config.php file, you need to access the server where the file is located. You have two options:
- Connect to your server through FTP and find the file. You must download the file, update it, and upload it back to the server.
- Use a File Manager that is usually available inside a control panel that allows you to manage your space inside the server.
One of the more popular solutions hosting companies use is a software called cPanel. You need to log in to cPanel and look for the option File Manager under the Files category.
You can go to the folder and edit the file directly on the browser.
The name of your main WordPress folder may vary depending on the hosting company that you choose, but usually, it will be called “public” or “www”. If you are having trouble finding it, contact your hosting company.
Before starting, make a backup copy of the file. It’s a simple change, but it’s always recommended to backup first.
Add the following line of code that defines a new PHP constant:
below the text /* Add any custom values between this line and the “stop editing” line. */
Save the file, and now wp-cron will not execute after a visit to one of your website pages.
How to Setup Manual Cron Job for wp-cron.php
Now, we need to make sure that the wp-cron.php script still runs when we need it. WordPress has scheduled tasks that need to be executed every day.
I’ll use cPanel again for this example. After logging in to cPanel, select a tool called Cron Jobs located inside Advanced category.
There, you have an option to add a new cron job. You can specify the timing and the command that will run at that moment.
Regarding timing, for most websites, once per hour is more than enough. And we want to run it daily, so update all options like the screenshot below.
Add the next line of code to the Command box:
wget -q -O - https://yoursite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Don’t forget to replace yoursite.com with the actual domain of your website.
Click the Add New Cron Job button to save your new cron job.
You added a new cron job, and if everything works, your wp-cron.php script will run once per hour every day.
How to Know If Manual Cron Job is Working
If you set your cron job to run once every hour, wait a bit longer than the interval you set.
Check the cron events again and see if any are overdue. If yes, there is something wrong with the cron job.
Check everything you did to see if something was done wrong, and if you are in need of more help, contact your hosting company to help solve the problem.