So you have a WordPress site which is fully optimized with a caching plugin, a recent and fast PHP version, maybe even MySQL and webserver optimizations, but still occasionally you come across a slow hit on your website. A very common, but not very well known, cause of this problem, is the WordPress cron.
What is a cron
On Linux/Unix systems, the cron is basically a task scheduler. You can schedule tasks/commands that are performed every once in a while, specified by a time definition. For example you can schedule something every minute, every 5 minutes, every second day of the week, a specific day every month, and so on. Many online applications, for example WordPress and Magento, also have their own built-in cron.
What is the WordPress cron doing
The WordPress cron is performing tasks that need to be done regularly. For example if you have plugin that shows a Twitter feed on your website, the cron updates this every x minutes or hours. Same thing for caching plugins, some plugins invalidate the cache daily and this is performed by the cron.
The cause of slow hits: how is the WordPress cron started
The WordPress cron is started in the background, when someone is visiting a website. The cron is not started with every visit, it's started when needed. But when it's started it needs to do an extra HTTP request to your server, which makes the initial request of the visitor on your website slower. It might be just a few milliseconds if the cron responds very fast, but it can be up to a second because of some technical reason in cURL where it waits. And a second is a lot.
How to fix this
The way to fix this, is to run the cron as background job on your server, instead of through your webserver spawned by a page request from a visitor. This is done by creating a cron job in Linux/Unix, or a Task Scheduler job on your Windows server. And by disabling the default WordPress cron.
Step 1: Add the cron as scheduled task
I will give you an example for a cron job in case you have a Linux/Unix server. You can edit the cron by logging in with SSH and type 'crontab -e'. If you have a control panel (such as DirectAdmin), you can edit the crons there. If you are using a shared hosting service and you don't know how to change the cron, just ask your hosting provider.
Example cron job that runs the WordPress cron every 5 minutes:
*/5 * * * * php -q /pathtowebsite/wp-cron.php
If for some reason the CLI version of PHP is not available, you can still run it through the webserver yourself, without slowing down visitors:
*/5 * * * * wget -q -O - https://www.website.com/wp-cron.php?doing_wp_cron
In both examples, don't forget to change the path to your website or the domain name of the website. Make sure you provide the full path in the first example, which is probably something like /var/www/vhosts/website.com/public_html/wp-cron.php or /home/website/website.com/wp-cron.php.
Step 2: Disable spawning the cron from visitor's page requests
To disable this function in WordPress and make sure that the WordPress cron is only started from your own cron task in the scheduler from your operating system, edit the wp-config.php in the root of your WordPress website and add this line:
You can add it simply anywhere after the first line (<?php). After this, save the file and check if your site is still working. If it is, you have done a great job 🙂