A cron job is a server command that automatically executes at a predefined time. In the context of WordPress and other content management systems, cron jobs are useful for tasks like updating plugins every two weeks, running a database optimization routine every other night at 2 AM, exporting a CSV file containing sales data every 4 hours, and more.
How WP-Cron Works
WordPress has a native cron implementation called WP-Cron. Unlike a traditional cron scheduler that references the server time, WP-Cron checks for any pending cron jobs on every page load. On high traffic sites, WP-Cron can have a significant impact on performance because it runs on every page load. On low traffic sites, WP-Cron can occasionally be unreliable because it only runs when a page is loaded. If a site doesn’t get much traffic, it’s possible for WP-Cron to miss scheduled tasks.
How to Disable WP-Cron
The solution is to disable WP-Cron, and set up a server-level cron job to trigger wp-cron.php
at a predefined time interval. To disable WP-Cron, you can add the line below to your wp-config.php
file.
define('DISABLE_WP_CRON', 'true');
How to Add a Server Cron
Now that WordPress’ native cron scheduler has been disabled, you’ll have to create a server cron to trigger wp-cron.php. There are a few different ways to accomplish this depending on your web host.
- If you’re using a managed WordPress hosting provider, you can reach out to support and ask them to add a server cron to trigger wp-cron.php. If they ask you for a time interval, either 10 or 15 minutes should be fine.
- If you’re using CPanel host, you can follow this guide to add a server cron.
- If you’re self-managing your server, you can use
crontab -e
in SSH to add the cron job.
Now that you know how to add a server cron, you need to generate the code snippet to add. To do this, I recommend using a cron job generator like this one. After going through the time interval settings, you’ll need to specify a command to execute. You can use the command below to trigger wp-cron.php
.
curl -ILs -H https://yourdomain.com/wp-cron.php?servercron
After running the command above through the cron job generator, you should see something like that looks like this.
*/15 * * * * curl -ILs -H https://yourdomain.com/wp-cron.php?servercron >/dev/null 2>&1
This cron job triggers the wp-cron.php
file every 15 minutes. Please note that the ?servercron
query string is optional. I included it because it makes combing through server logs to search for cron requests a little easier. After you add the cron job, keep an eye on your server logs for the ping to wp-cron.php. Disabling the native WP-Cron implementation can give your site a noticeable performance boost, and I recommend putting it on your list of performance optimizations to do on you WordPress site.