WordPress cron – scheduling tasks in wordpress

July 23, 2013 — 2 Comments

Check out the video for an overview of the wordpress cron system. Here’s some background information:

Sometimes on a wordpress site you need to run a job at regular intervals. Say, for example, you store draft customer details when people are browsing your site; and you want to remove them if they haven’t completed their registration after a couple of days. Now, there is a good system in Unix OSes called cron that will do this, but it’s not always suitable…

Why would you want to set up a WordPress cron job?

  • Some hosting doesn’t support cron
  • Setting up cron jobs is a manual step, and when you move hosting at some future date you will have to remember to recreate the cron jobs on the new hosting
  • …and finally, when the PHP in question is part of a PHP plugin that you want to distribute for general use, you really want the plugin to be self-configuring. I mean, you can’t just put a plugin on the wordpress codex and ask people to jump through extra hoops for installation: you want the plugin to set itself up automatically. As a half measure, setting up a unix or other non wordpress cron job from within your wordpress plugin activation code is possible but not straight-forward and is likely to fail on many web servers.

So it’s much better if we can set up our wordpress plugin to do all this for us. Here are the key features we’re going for:

  • The wordpress cron job gets setup when the plugin is first activated and will only be setup once
  • The job runs after a scheduled interval, but the next time a page is loaded. This means that if you set up a job to run hourly but no-one views your site for 3 days, the job won’t run for 3 days.
  • I am going to implement this as an otherwise empty WordPress plugin, but it could also work from inside a theme’s functions.php. IF you want to do that, you’ll need to rework it to not hook into the plugin activation event.

Read those over a couple of times and make sure that this style of scheduled job will suit you; particularly given the second point! The gotcha there will really only affect you if your site gets a very low amount of traffic.

Ok, but how do we do it?

First, let’s set up some boilerplate empty WordPress plugin code
1. Ok, create a new empty folder at /wp-content/plugins/glpschedule/.
2. Now create /wp-content/plugins/glpschedule/glpschedule.php. Put this in it:

We’ve got an empty plugin. Now paste this in:

Plugin Name: Jay's PHP Generic Blah
Plugin URI: http://golearnphp.com
Description: Just another wp plugin feature demo
Version: 0.1
Author: Jay Docherty
Author URI: golearnphp.com
// Let's do an hourly check
add_action('wp', 'activateMe');
function activateMe() {
	if ( !wp_next_scheduled( 'hourly_check' ) ) {
		wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'hourly_check');
add_action('hourly_check', 'removeDraftRegistrations');
function removeDraftRegistrations() {
	// do something every hour
    global $wpdb;
        $sql = "DELETE FROM jbcustomer WHERE
        payment_state = 'Unpaid' AND
        dt_modified < (date_sub(now(),  INTERVAL 6 HOUR));";
    $results = $wpdb->query($sql);  

Let’s go through this:

On line 13 we add a listener for the wp event. That gets triggered when WordPress starts (ie when a WordPress page is requested). The listener calls function activateMe

Lines 15-19: we define activateMe: check that we haven’t already set up our new wordpress cron (ish) job ‘hourly check.’ If not, we add it, setting it to run hourly, and to call a new action called ‘hourly_check.’

Line 21 we define this new action, and make it call a new function called ‘removeDraftRegistrations’.

From line 23 we define that function. This is where you will need to do your own customisation. As an example, mine removes draft member registrations from the database.

Questions via the comments form please. Cheers!

2 responses to WordPress cron – scheduling tasks in wordpress

  1. If you need a trigger to your cron script, easycron.com works great.

Leave a Reply to Jay Cancel reply


Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>