Updates & Writing
Illustration of a man standing next to an hourglass

Improve WordPress performance by disabling WP‑Cron

CRON jobs are a way of scheduling server tasks to occur at a specific time or interval.

WordPress runs WP‑Cron which simulates this process.

WP‑Cron fires every time a page loads and checks whether there are any cron events that need to run. If there are any events that are due, WordPress will run them.

For high-traffic websites, this can be an unnecessary performance hit.

The better approach is to disable WP‑Cron and use the proper system cron instead.

Disabling WP‑Cron

This is the easy part, disabling WP‑Cron. Edit wp-config.php and add the following line anywhere before the comment “That’s all, stop editing! Happy blogging.”:

define('DISABLE_WP_CRON', true);

How often should cron run?

The frequency of your cron schedule depends entirely on the complexity of your website and the type of features and plugins you have.

A standard WordPress installation has eleven different core events that run on the WP‑Cron schedule. Most of these tasks are non-essential and you could easily decide to run them less frequently. The most frequent task runs hourly and removes export files that are more than three days old. Other tasks, run daily and twice daily, check for plugin and theme updates, delete expired transients and run other maintenance tasks.

Once you start adding plugins you may find that your site uses the WordPress Action Scheduler which runs every minute and processes a centralised job queue for a range of different plugins.

For a simple website, you might only need to run your system cron once a day. For a more complicated website, you might want to schedule cron every minute.

The simplest way to determine your cron schedule is to look at the current events and schedules on your website. The easiest way to do this is by installing the WP Crontrol plugin.

Calculating cron schedules

Once you’ve determined the frequency of your WordPress cron jobs, you’ll need to craft an expression for the cron schedule.

The cron syntax can be confusing. A cron expression is made up of five fields. Each field can have the following values.

  1. minute (0-59)
  2. hour (0-23)
  3. day of the month (1-31)
  4. month (1-12)
  5. day of the week (0-6)

The easiest way to understand and test your expressions is to use this helpful calculator.

Setup System Cron

Assuming you have SSH access to the server, you’ll need to add a line to your crontab. Access your user crontab:

crontab -e

(If you don’t have SSH access you can usually add cron jobs via your hosting panel. Here are the instructions for cPanel and here are the instructions for Plesk.)

There are a couple of different ways to trigger the cron event and you will only need to use one. Listed below are the PHP and cURL methods, although you could also use wget.

PHP

*/15 * * * * /usr/bin/php /home/user/www/example.com/html/wp-cron.php > /dev/null 2>&1

Breaking it down, every fifteen minutes cron will use a PHP process to run your wp-cron.php file. No email notifications are sent. For performance, using PHP directly is usually the best method since it doesn’t take up a network slot on your webserver.

cURL

*/15 * * * * curl -s -o /dev/null https://example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

The difference here is that cURL will trigger an HTTP request to access wp-cron.php.

cURL is often the easier option to configure since it runs with webserver permissions (www-data) while executing PHP will be run by the cron user and you might run into issues due to conflicting permissions.

Need assistance?

If you don’t feel comfortable setting up cron jobs on your server or through your hosting provider, get in touch. I provide maintenance packages and can manage your WordPress website for you.