WordPress Cron Job setup with WP-Cron.php

When setting up a cron job for WordPress we need to address a few issues. Do you really need to worry about setting up a separate cron job? Once you set it up, how do you stop it from sending you an email everytime? And once you set it up, how do you stop it from filling up your server with files?

WHY DO YOU NEED A SEPARATE CRON JOB FOR YOUR WORDPRESS SITE?

So the issue is, that by default, everytime somebody views a page on your WordPress site, the wp-cron job is run. This is not really an issue if you have less than 60 page views per hour (or 1 page view per minute). When you have a low traffic site, then there is not much else going on that wp-cron can interfere with. However IF you are getting more that 60 page views per hour, then it can start to become a much bigger issue and really slow your site down.

Lets consider an example where you are a high traffic site getting about 3,000 page views per hour (50 page views per minute). This would mean that wp-cron is being called and run for every single page view, meaning its being called and run 3000 times an hour. What if you get 30,000 page views per hour? Well now it is really interfering and causing a problem.

SETTING UP A CRON JOB

There are a multitude of sites that will teach you how to setup a cron job, so I will not go into all the details here. I will only briefly give you the two methods that you can use with their command line argument that you need.

Disbable wp-cron on page view

Regardless of which method you use, you will need to disable the wp-cron from running everytime somebody views a page on your site. You will need to edit your wp-config.php file and add the following lines at the top of the file. You can actually add them anywhere in the file really, just as long as they are after the  <?php  tag on the first line.

//Disable internal wp-cron function - setup external cron job instead.
define('DISABLE_WP_CRON', true);

Method #1 – WGET (recommended for WordPress)

The preferred way to setup an external cron job for WordPress is to use the WGET method. Make sure you include the --delete-after option otherwise your server will fill up with thousands of files named wp-cron.php.1 , wp-cron.php.2 , etc.

Use this line:
wget --delete-after http://mydomain.com/wp-cron.php

If this method keeps sending you an email everytime it runs, then you can stop the email by sending the output to a file instead with the --output-file option. This will only ever create one small file on your server as it will overwrite itself each time and will therefore not get big.

Use this line:
wget --output-file=cronlog_wp-cron.txt --delete-after http://mydomain.com/wp-cron.php

Method #2 – PHP Command Line Tool

The other method is to use the PHP Command Line tool from the cron. This is usually my preferred way to setup most cron jobs, however this creates an error in the WordPress wp-cron.php file where a require_once fails to load wp-load.php so it is not recommended.

Use this line:
/usr/local/bin/php -f /home/myroot/public_html/wp-cron.php

You may have to ask your web host where the php command line tool is located and adjust the /usr/local/bin/php accordingly. Also, you may have to ask your web host what your /myroot/ should be as well.

And if this method keeps sending you an email everytime it runs, then you can stop the email by sending the output to /dev/null instead.

Use this line:
/usr/local/bin/php -f /home/myroot/public_html/wp-cron.php > /dev/null

 

Please let me know if this was helpful by commenting below.

6 thoughts on “WordPress Cron Job setup with WP-Cron.php

  1. The relative path in require_once() issue in wp-cron.php has been fixed, so you don’t have to worry about using the absolute path any more.

  2. @Steve- Personally I prefer not to change the core files though because it makes updates harder because you have to remember to go back and change it everytime you update WP. But thanks for the info as some may want to use that method.

  3. You can use method two without errors if you remove the “./” before wp-load.php in wp-cron.php on line 27.

Leave a Reply