Performance

Why You Should Ditch WP-Cron: A Guide to Using Normal Cron Jobs with WordPress

August 1, 2024
|
Read Time: Estimating...
By Chris Ard

In this article:

Share this on:

What are Cron Jobs?

In 2024, WordPress is still easily the most popular Content Management Systems out there. However, despite being so widely adopted, it does a few things in an outdated or peculiar manner, especially in how it handles cron jobs.

Cron jobs are the essential tasks that run behind-the-scenes according to a schedule. For example, clear my cache every 5 minutes, or check for updates every X. Normally, a cron is run programmatically against a universal time, ensuring the task is always run predictably – WordPress does this differently. In this article, we’ll delve into the world of crons, explore why using a normal cron instead of WP-cron is beneficial, and provide step-by-step instructions on how to switch from WordPress’s default cron handling.

A cron job is a Unix-based system that allows users to schedule tasks or commands to run at specific times. This powerful feature enables automation of repetitive, time-sensitive processes – such as updating plugins, sending newsletters, and clearing cache.

Cron jobs are typically set up using the crontab (cron table) file on your server, which specifies when a task should be executed. Each line in the crontab represents an individual cron job with its own schedule and command to run.

How WordPress Handles Cron Jobs

Before we turn anything off, we should start by understanding how WordPress cron actually works. By default, WordPress utilizes WP-cron – it’s own version of cron and a built-in feature that schedules tasks using PHP’s cron API. While convenient for many users, this approach has several limitations:

  1. Inaccurate scheduling: Due to the nature of PHP scripts running on demand, cron jobs can be triggered at unpredictable times or even skipped altogether.
  2. Resource-intensive: WP-cron relies heavily on server resources (CPU and memory), which may lead to performance issues if left unchecked.
  3. Security concerns: WordPress introduces potential security risks through misconfigured cron jobs. For example, DDoS attacks in the past have been able to cripple sites by exploiting it’s exposure to the public.

Resources:

Scaling Problems with WP-Cron

Worst of all, WP-cron loads every time a user visits your website – leaving scheduled tasks up to visitors rather than the application technically. This can be detrimental to:

  • Small sites: Inaccurate scheduling and missed executions due to low traffic.
  • Large sites: Performance issues from resource-intensive processes triggered on each visitor, resulting in possible bottleneck conditions and unnecessary calls.

Benefits of Using a Normal Cron

Switching from WP-cron to the native Unix-based cron job system offers numerous advantages:

  1. Accurate scheduling: With normal crons, tasks run precisely at their scheduled times – eliminating any uncertainty or missed executions.
  2. Improved performance: Native crons offload resource-intensive processes onto your server’s scheduler, minimizing impact on PHP execution and overall website speed.
  3. Better security: By decoupling cron jobs from WordPress’ core functionality, you mitigate potential vulnerabilities associated with WP-cron.

With normal crons, tasks run precisely at their scheduled times – eliminating any uncertainty or missed executions.

schedule project

Deactivating WP-Cron in wp-config.php

To begin transitioning to a normal cron setup:

  1. Open your wp-config.php file and locate the following line if ( ! defined( 'ABSPATH' ) )
  2. Add this above it: define('DISABLE_WP_CRON', true);

So the result would look something like:

define('DISABLE_WP_CRON', true);

/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
	define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

Setting Up a Cron on Your Server (Common Platforms)

The specifics for setting up crons vary depending on your server platform, as you may even have service that does this for you like cPanel or equivalent. However, it is more or less the same for any of them:

Ubuntu/Debian-based systems

  1. Open the terminal and navigate to /etc/cron.d.
  2. Create a new file named your-site-name or something similar, e.g., my-wordpress-crons. The contents of this file will define each cron job.
  3. Add lines in the following format:
# m h dom mon dow command
*/5 * * * * /usr/bin/php -f path/to/wordpress/cron.php > /dev/null 2>&1
  • Replace your-site-name with your actual domain or website name.
  • Update /path/to/wordpress/cron.php to point to the WordPress cron script (usually located in the site’s root directory).
  1. Save and exit.

CentOS/RHEL-based systems

The steps are similar, but you’ll need to use crontab -e instead of creating a new file:

  1. Open the terminal.
  2. Run: sudo crontab -u your-username -e
  3. Add lines in the same format as above.

Other platforms (Cpanel, Plesk)

Other platforms often have Cron Jobs built into the interface, so it would just be a matter of selecting the time interval and entering the path to the file as before:

# m h dom mon dow command
*/5 * * * * /usr/bin/php -f path/to/wordpress/cron.php > /dev/null 2>&1

Cpanel is a very popular one, and most other services will follow a similar configuration method. You can learn more about it using their documentation.

Resources:

Wrapping Up

Using a normal cron instead of WP-cron offers numerous benefits, including more accurate scheduling, improved performance, and enhanced security. By following the steps outlined in this article – disabling WordPress’ default cron handling and configuring native Unix-based crons – you’ll unlock greater control over your website’s automation tasks.

Don’t let inaccurate scheduling or resource-intensive processes hold back your website’s potential. Take the first step towards a more streamlined, secure, and reliable online presence by implementing normal cron jobs.

If that all sounds too complicated to you, know that as a part of our builds, we take care of this along with a whole list of other optimisations so you don’t have to. Reach out to our team for personalized assistance with performance optimisation for WordPress.

Share this on:
(This article was last updated on September 6, 2024)

Ready to get started?

Transform your online business with cutting-edge web solutions that promise speed, reliability, and security.

Experience bespoke website design, premium hosting, and stress-free maintenance — all expertly crafted to your needs. Reach out now for a tailor-made digital experience that sets you apart!

You selected

Awesome! We just need a few details first ...

Help us get to know you and your requirements with some basic info.