What is the WordPress cron?
WordPress uses a system similar to the Unix cron that runs scheduled tasks like checking for updates, applying automatic updates, publishing scheduled posts, etc. The WordPress cron does not always run on the server like the Unix cron; it works by loading the wp-cron.php file when a visitor accesses a page of the WordPress website.
What are some benefits and downsides of the WordPress cron?
The default behavior of relying on visitors to access a page of a WordPress website comes with some benefits and downsides. The main benefit is that the WordPress cron will be checked regularly for jobs that should be executed if there are a lot of visitors on the website. This is crucial for high-traffic websites that rely on retrieving timely updates or data through plugins via the WordPress cron as the data presented to the website visitors will be up-to-date. This may also backfire as your WordPress cron may not run for extended periods if there are no visitors on your website for some time. The WordPress cron will run after a visitor arrives at your website; however, depending on the time it takes for the WordPress cron to finish, the visitor may see inaccurate data. It is also important to mention that multiple instances of the WordPress cron can run simultaneously when they take too long to finish, so this may additionally delay the execution of the scheduled tasks. Another big downside of the default WordPress cron execution mechanism is that it may result in an increased usage of system resources when there are a lot of visitors on the website (if your website is crawled by bots for example). Another downside is that the WordPress cron may actually increase the page loading time for your visitors and negatively impact their experience on your website.
How to reduce the WordPress cron execution interval?
To try and reduce the downsides of the default WordPress cron mechanism, you can add the following line to the WordPress configuration file:
define( 'WP_CRON_LOCK_TIMEOUT', 3600 );
This line sets the minimum execution interval of the WordPress cron to one hour, which is usually sufficient for most websites and WordPress scheduled tasks, but still helps significantly in reducing the usage of system resources and the negative effects on your website's page load times.
All WordPress applications pre-installed on the hosting account or installed through the hosting Control Panel > WordPress Manager will have this line added to the WordPress configuration file by default.
How to replace the WordPress cron with a cron job?
To replace the WordPress cron with a regular cron job on our servers, you need to:
- Disable the default WordPress cron execution for page visits. You can do this by adding the following line to your WordPress configuration (wp-config.php) file:
You can use the hosting Control Panel > File Manager section to do this.
- Create a wrapper script in the directory of your WordPress installation (e.g. /www/www/cron.sh if WordPress runs on your main domain) that will execute the WordPress cron. Again, you can use the hosting Control Panel > File Manager section. There are many ways you can execute the WordPress cron, so we will list only two examples - by sending a GET request to the wp-cron.php file and by using WP-CLI.
To have the wrapper script run the WordPress cron by sending a GET request to the wp-cron.php file (effectively visiting it on your website), add the following code block to your wrapper script:
GET "http://YOUR_DOMAIN.com/wp-cron.php" > /dev/null
Make sure you replace YOUR_DOMAIN.com with the exact domain/subdomain where your WordPress installation is running.
To have the wrapper script execute the WordPress cron via WP-CLI, use this code block in your wrapper script:
wp cron event run --due-now > /dev/null
If you have the WP-CLI wrapper script outside your WordPress directory, you will need to explicitly specify the WordPress directory with the path option:
wp cron event run --due-now --path /home/example/www/www > /dev/null
In the above example, WP-CLI will run all pending cron events in the hosting account with username example for the WordPress installation of the main domain (in the ~/www/www/ directory).
- Navigate to the hosting Control Panel > Cron Jobs section and create a cron job that executes the wrapper script once per hour or, if necessary, more frequently based on your needs and the available cron execution intervals for your hosting plan:
You may see a warning displayed for the cron job indicating that the wrapper script is missing execute permissions. To fix this, simply use the "Click here" link in the warning message:
- The WordPress cron will now run correctly at the specified interval to execute any pending scheduled tasks without relying on visitors to access your website and without increasing the page load time when visitors actually access and use your website.