CRON and WP Rocket
This article covers all the relevant information about cron on WordPress and its relation with some WP Rocket's features, as well as instructions to set up cron job in the server, and a common issues section.
What is cron?
Cron is a standard utility in Unix and Linux systems, it is used to schedule commands for automatic execution at specific intervals. For instance, you might have a script that produces web stats and you want it to run once a day at a specific hour. Or imagine you need to run a PHP script on weekdays, every 2 hours.
You can't do these tasks manually and here is where Cron becomes useful. These commands involving Cron are referred to as " cron jobs."
WP-Cron vs "real" cron jobs
WordPress has a feature called WP-Cron. WP Cron is a built-in function for automation which allows to schedule automated tasks, like publishing posts, maintenance tasks, update checks and by many plugins like WP Rocket which need to automate features.
WP-Cron is not actually a real cron job, you can think it is more like a “fake cron job”. These tasks are triggered when someone visits your site: during PHP page loads, WP Cron checks the database to see if there are any scheduled tasks to execute.
The way WP Cron works has some drawbacks:
- When you are using a caching plugin like WP Rocket, no PHP is executed in the frontend, so if there is no activity on WP Admin for some time, WP Cron processes would stop being triggered.
- It is also a potential issue on high traffic sites, since every page request might spawn a process that uses server resources
- This file can be used as the target of a DOS attack
Tip: To check if WP-Cron can run on your WordPress site: WP-Cron Status Checker
After activation, visit your Dashboard and look for the WP-Cron Status Checker widget.
How to check if cron is running correctly
Whether you're using WP-Cron or a server side cron job, you should make sure the frequency of occurrence is reliable and high enough, as shown in this article:
Setting up a real cron job single installations
Due to WP Cron drawbacks, it's always recommended to set up a real Cron job on your server instead of relying on WP Cron.
- If you want to create a real Cron job, it is recommended to disable WP Cron, so is not executed every time someone loads one of your pages. To disable it, open the wp-config.php file in your main WordPress folder and add the following line before the "/* That's all, stop editing! Happy blogging. */" line:
define('DISABLE_WP_CRON', true);
- Add a Cron job entry through your web hosting control panel. You can set the job to run every 1 or 5 minutes.
Please remember to replaceyourdomain.com
with your actual domain:
wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Each server has different ways of adding a Cron job to execute PHP scripts, so if you are not sure how to do it you can ask your hosting provider to help you. For example, in some cases, you should use the PHP command instead of WGET:
php -q /home/path/public_html/wp-cron.php
In the above example, the path to the wp-cron.php file will be different on your server so please update it accordingly.
Setting up a real cron job for multisite
If you are using WordPress Multisite, you need to trigger the CRON on each site of the network.
- If you want to create a real Cron job, is recommended to disable WP Cron, so is not executed every time someone loads one of your pages. To disable it, open the wp-config.php file in your main WordPress folder and add the following line before the "/* That's all, stop editing! Happy blogging. */" line:
define('DISABLE_WP_CRON', true);
- Create a file on your WordPress root folder called
custom-cron-multisite.php
and add the following code to it.
<?php require('wp-load.php'); if ( is_multisite()) { $sites = get_sites(); foreach ($sites as $site ) { $site_url = get_site_url($site->blog_id); switch_to_blog( $site->blog_id ); wp_suspend_cache_addition(true); $command = $site_url.'/wp-cron.php?doing_wp_cron'; wp_remote_get( $command ); wp_suspend_cache_addition(false); restore_current_blog(); sleep(3); } }
If you have a lot of websites on your network, you might need to extend the
sleep()
function (the waiting time between two consecutive network site CRON triggers) by increasing the duration. This can be done by increasing the3
to your desired waiting time value. - Add a Cron job entry through your web hosting control panel. You can set the job to run every 1 or 5 minutes.
Please remember to replaceyourdomain.com
with your actual domain:
wget -q -O - https://yourdomain.com/custom-cron-multisite.php?doing_wp_cron >/dev/null 2>&1
Setting up an external cron job
Sometimes, setting up a server-side cron is not possible or limited due to hosting service restrictions or doesn't work as expected.
In such a case, you can set up an external cron using a free service like cron-job.org.
Follow these steps:
- Create an account at - https://console.cron-job.org/signup
- Log into the dashboard
- Got to - https://console.cron-job.org/jobs
- Click the "Create Cronjob" button
- Set the cron file URL - https://example.com/wp-cron.php (change the domain name to fit your website's)
- Adjust the periodicity to "Every 2 minutes" from the drop-down
- Click the "Create" button on the bottom for the form
Common issues related to cron
If for any reason, scheduled processes are not properly working, you may notice issues with some of the following WP Rocket features:
- Cache Preloading
- Scheduled Cache Clearing
- Database optimization
- Critical Path CSS generation for Load CSS Asynchronously
- Remove Unused CSS
- Cloudflare add-on
- Dynamic lists of exclusions and inclusions
Did you know?
You can extend WP Rocket functionality using Cron to launch custom functions.
For example: How to clear cache via cron job