WordPress, code, design, business

Using a Real WordPress Cron Job for Increased Reliability

Sometimes it’s useful to setup a real cron job to ensure that scheduled WordPress core and plugin maintenance gets done. There are things that happen automatically behind the scenes that keep your site running as expected (publishing scheduled posts, for example). In some cases these tasks may not be running reliably. Here’s how to solve that.

What is a Cron Job?

I’ll just say this instead of pasting some semi-technical block of text from Wikipedia.

Scheduled stuff happens automatically.

A cron job needs to be setup manually. In most cases, you log into your hosting control panel and schedule a command to be executed at certain times (every 15 minutes, midnight, Tuesdays, etc.). A standard installation of WordPress doesn’t use a real cron job.

Why Use a Real Cron Job?

I’m writing this tutorial because I learned that caching plugins can naturally interfere with WordPress’s ability to automatically run its scheduled tasks. My Church Theme Content plugin has recurring events (nobody wants to re-enter Sunday Services every week) and I found that the dates on these events were not automatically moving forward every time. I wanted to keep using the caching plugin so the solution was to force the scheduled tasks to run by setting up a real cron job.

Instead of having users setup a real cron job during installation, WordPress has a sort of “virtual cron” feature. Basically the way it works is that you have visitors to your website and periodically one of these visits causes WordPress to ask itself, “should I run some task?” and if the task is due, it runs. This works in most cases but using a real cron job to trigger WordPress tasks is more reliable, especially on low traffic sites (no visitors to trigger tasks) or if you’re using a caching plugin (no trigger since a cached copy is served).

Setup a Real Cron Job for WordPress

I will use cPanel in this example since there is a very good chance your web host uses it. Setting up a cron job with other hosting control panels such as Plesk should be similar (in some cases the feature may be called “Scheduled Tasks” or similar).

  1. Log into cPanel
  2. Under Advanced, click on Cron jobs
  3. Under Add New Cron Job, choose “Once an hour” for Common Settings
  4. Enter this for Command (changing the URL to match your site):

    wget -O /dev/null http://yoursite.com/wp-cron.php?doing_wp_cron

  5. Click Add New Cron Job

cPanel WordPress Cron

What this does is trigger WordPress’s wp-cron.php script every hour (you can make it more frequent). Any tasks that WordPress has scheduled to run will run (publish a scheduled post, see if my plugin’s recurring events need to be moved forward, etc.). You are no longer relying on WordPress’s “virtual cron” to be triggered upon a site visit.

Disable WordPress “Virtual Cron”

I have not found this to be absolutely necessary but you can do this to disable WordPress’s attempts to run “virtual cron” when visitors access your site. At the very least this will reduce the load on your website. This may be worthwhile for very high traffic sites.

Simply edit your wp-config.php file to add this:

define( 'DISABLE_WP_CRON', true );

What About Multisite Networks?

If you’re running a multisite network installation of WordPress you’ll need to setup a real cron job for each site, including the main network site.

Main Site

The URL to use for the main network site would be:

http://yournetwork.com/wp-cron.php?doing_wp_cron

Subsites

The URL for each subsite would be like one of these, depending on if you use subdirectories or subdomains.

http://yournetwork.com/subsite/wp-cron.php?doing_wp_cron

http://subsite.yournetwork.com/wp-cron.php?doing_wp_cron

You would make a cron job for each subsite in addition to the main network site.

Conclusion

This is usually not difficult to setup but I found few guides out there that I was comfortable linking my customers to, so I wrote this. Hopefully this has been useful to people using caching plugins or that have other reason to force cron to run on WordPress. Please post a comment if you have any suggestions to make this guide more useful.

Related Resources

Tweet about this on TwitterShare on Google+Share on Facebook

3 Comments

    • Thanks May. One of the reasons WordPress uses “virtual cron” is because not all hosts provide users with access to cron jobs, so this is handy. I see you can schedule three cron jobs for free. That’s should be enough in most cases.

Add a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>