How to replace WordPress Cron with Real Cronjob for Site Speed
How to replace your WordPress Cron job with a server Cronjob 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 cronjob 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 cronjobs 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! 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 it’s cron job|
The code simply defines
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
PHP but you can use other, similar commands if you prefer to change it. Here is the command that you’ll use:
php -q /home/username/public_html/wp-cron.php