Replace WordPress Cron with Real Cron for Site Speed

How to replace your WordPress cron job with a server cron job to increase site speed.

We can all relate to a slow WordPress website with a very long request times. Most of us have also seen the WordPress “Missed Schedule” error message for content such as future scheduled posts that were supposed to be published but weren’t. The list goes on and the WordPress cron job is the cause.

Inexplicably slow WordPress sites with high CPU usage is frustrating. You need a break so let’s fix this!

How Does the WordPress Cron Work?

In reality, the WordPress cron is a system that has a set list of tasks, each with a time when it needs to be executed and it’s once or recurring status.

When the time comes – or has lapsed – the WordPress cron executes the task and continues with the list.

Visitor traffic is required for WordPress to keep track of time and scheduled tasks to execute with the cron.

Why Would the WordPress Cron Fail?

There are many reasons why a cron job could either fail or be delayed:

Server overloaded and as a result the cron cannot execute.

Not enough or not frequent enough visitors on the website.

A bug or conflict caused by a plugin or combination of plugins.

and so on…

Benefits of Using a Server Cron Job Instead

Replacing your WordPress cron job with a real, server cron job is beneficial in many ways:

Reliable, accurate cron jobs that fire on time as expected

Make your WordPress website faster to your users/visitors

Eliminate high CPU usage caused by WordPress

Both benefits are important but the 2nd one is especially important.

Unfortunately, when someone visits your website and the WordPress cron has a task to run at that moment, it will run that task on the visitor’s time, increasing the time of the request or even ending up giving that visitor a blank page if the task cannot complete.

So you take the cron jobs off your visitors and relaying it to the server side of things.

How to Replace the WordPress Cron?

This is quick and easy – let me show you how in 2 steps:

1. Disable the WordPress Cron Job

First, disable the WordPress cron job by opening your wp-config.php file and placing the following line of code into it at any place inside the opening PHP tag.

// Place the line of code below in your wp-config.php file

// Defining the constant as true will tell WordPress to stop executing its cron job

define('DISABLE_WP_CRON', true);

(https://gist.github.com/tribulant/ea6bd43800f48f91453b)

The code simply defines DISABLE_WP_CRON to true which WordPress knows of and will then not run cron jobs itself but wait for them to be called instead.

2. Create a Server Cron Job

With no WordPress cron job running anymore, you need to setup a server cron job to replace it. The server cron job will not actually run the scheduled tasks directly, it is just doing the job that your visitors have done up until now, telling WordPress what the date and time is so that it can check if tasks should be fired.

The instructions on setting up a cron job on your hosting will depend on your hosting control panel interface. I would say the most common control panel for hosting is cPanel which is used by most large hosting companies these days and it is great.

So login to your cPanel and go to Advanced > Cron Jobs.

Then under “Add New Cron Job” you can create the cron job. I recommend that you use an interval of 5 to 15 minutes depending on your website and what it does. Use the “Common Settings” drop down menu to select your interval for the cron job.

The best command to use is WGET but you can use other, similar commands if you prefer to change it. Here is the command that you’ll use:

wget -q -O - http://www.yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

(https://gist.github.com/tribulant/5ffc5c7834c4497bb32c)

Be sure to change out the www.yourwebsite.com part with your own domain or URL to your WordPress home page.

WordPress Multi-Site Server Cron Job

Unfortunately the WordPress cron has to be run individually for each site/blog on the WordPress multi-site network.

Because of that, you’ll need to repeat the above process for each site/blog on the network. Or alternatively, you can create a new file named wp-cron-multi-site.php in the WordPress root (same location as the current wp-cron.php file) and put the following code into it:

<?php

if (!defined('ABSPATH')) {

require_once(dirname( __FILE__ ) . '/wp-load.php');

}

global $wpdb;

$sites = $wpdb -> get_results("SELECT `domain`, `path` FROM " . $wpdb -> blogs);

foreach($sites as $site) {

$url = "http://" . $site -> domain . ((!empty($site -> path)) ? $site -> path : '/') . 'wp-cron.php';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec($ch);

curl_close($ch);

}

?>

(https://gist.github.com/tribulant/2afd9afc09e5478b630e)

Then, simply change the cron tab command in cPanel to this:

wget -q -O - http://www.yourwebsite.com/wp-cron-multi-site.php?doing_wp_cron >/dev/null 2>&1

(https://gist.github.com/tribulant/d1581c54fef9f8dc6687)

WordPress Server Cron Job Not Working?

Unfortunately, some hosting providers have restrictions on the interval and some hosting providers do not even allow the WGET command to be executed. Sad, but it is happening, and it is a common issue. You have 3 possible solutions here:

Contact your hosting provider and ask them to enable WGET or provide an alternative Get proper WordPress hosting that won’t restrict you in unfair ways. Use a 3rd party, online cron job service such as EasyCron for your cron jobs.

And that’s it, you’re done! You’ve replaced the WordPress cron job with a real, server cron job. I hope that you found this useful and that it will increase the speed of your WordPress website drastically.

Shares









I am the owner at Tribulant Software with a great passion for WordPress, development, blogging and the Internet in general.

Like this: Like Loading...