In WordPress, there’s a small file called wp-cron.php, which simulates normal cron-jobs. With the exception that this file isn’t executed every at a certain amount of minutes. Instead, WP-Cron will be executed each time someone visits your website.
How is WP-Cron affecting the performance of your website?
If your website is receiving a high amount of traffic, and the WP-Cron is executed on every visit, this will cause multiple processes that are executed, to run the cron.
How to disable WP-Cron in the wp-config.php
WP-Cron can be easily disabled by adding the following code snippet into your wp-config.php file:
define('DISABLE_WP_CRON', true);
The dilemma with disabling the WP-Cron is, that some features of WordPress will stop running. E.g. – scheduled posts will not work, because it is directly dependent on the cron. And also other plugins that are hooked the WP-Cron will stop running.
A good way to fix this for a high traffic website is to disable the WP-Cron using the code snippet above, but in the same time to set up a cron job to run manually, from your control panel.
This can be easily done by adding a cron job and to set it to be executed at a certain amount of time. There are multiple ways to run the WordPress Cron, one of them is by adding this cronjob and executing the wp-cron.php file with PHP.
/usr/local/bin/php -q /home/$USER/public_html/wp-cron.php > /dev/null 2>&1
but the recommended way is to run the WordPress cron using WP-CLI and yes we offer WP-CLI on all of our servers, and here is how you can do it:
/usr/local/bin/php /usr/local/bin/wp cron event run --due-now --path=/home/USER/public_html/ >/dev/null 2>&1
To break this down:
/usr/local/bin/php
is telling the script to use the PHP binary to execute the command. You can confirm this is correct for you with the command “whereis php” from your Terminal in cPanel.
/usr/local/bin/wp
tells PHP to use the WP-CLI binary program.
cron event run --due-now
will ask WP-CLI to execute any tasks that are currently due.
--path=/home/USER/public_html/
As WP-CLI will execute in the directory that it is run from, we need to specify exactly where WordPress is installed. You will need to modify this line to match your installation.
>/dev/null 2>&1
this final part tells the full command to send any standard or error output to /dev/null so that your logs don’t get filled with erroneous data.
Now, you might ask how often this cronjob should run. Well, that’s a very good question, and we recommend to configure that running it every 15 minutes. This means it will run 96 times every day, which should be fairly enough.
Setting up the cron to run every 15 minutes will, first of all, lower the resource usage of your account and maybe even make your site load faster when you have high traffic coming in.
How to silence the cron output
By using this method, your default cPanel email account will receive an email notification each time the cron is run. If you prefer not to receive this email, you can update the cron job command and add at the end of the command this code, which will silence the output of the cron:
> /dev/null 2>&1
That’s a wrap!