Welcome to the first post in a series of posts on how to take control of the WP-Cron system using WP-Crontrol. WP-Cron is a tangle of black magic that allows a plugin developer or a user to schedule commands to be executed. WP-Crontrol is a plugin that lets a blog owner see through that magic and figure out what’s actually going on. In this post, I’ll discuss using WP-Crontrol to add a new cron schedule that can be used by WP Database Backup. This means backups when you want them.
Deep in the heart of WordPress, there’s a variable that stores the possible recurrence intervals (schedules) that a WP-Cron job can have. A stock installation comes with two: hourly and daily. Plugins can also add additional ones; this is what WP-Crontrol allows you to do in the Manage->Crontrol admin panel. Plugins use these intervals when scheduling cron jobs. If you only have an hourly and daily schedule, then you can’t run a cron task every other day, for example. And that’s the task that we’ll attack in this post: scheduling a backup every other day.
We’ll need two plugins to do the following steps. The first one, of course, is WP-Crontrol as that’s what this whole series is about. The second plugin you’ll need is WP Database Backup because that’s the focus of this post.
The Lay of the Land
As a first step, I recommend familiarizing yourself with WP-Crontrol and seeing what’s currently in the WP-Cron system. Goto the Manage->Crontrol admin panel and you’ll see the current cron jobs that are scheduled. Depending on the plugins that you use, the list may be empty; WordPress doesn’t make too much use of the cron system out of the box.
Cron schedules are stored in the Settings->Crontrol admin panel. There, you will see comes with WordPress (hourly and daily), as well as one that comes with WP-Crontrol (twicedaily) and one from WP Database Backup (weekly). There’s a chance that other plugins have added additional schedules also.
Now pop over to the WP Database Backup configuration in the Manage->Backup admin panel. At the bottom of the page, in the ‘Scheduled Backup’ section, you’ll see the same list of schedules. That’s no coincidence. The backup plugin pulls it’s list from the same place that WP-Crontrol does, namely the
cron_schedules filter. You’ll notice that if your desired interval isn’t in this list, you’re outta luck.
Adding the schedule
- Go back to the Settings->Crontrol admin panel. This is where cron schedules are managed, remember?
- Take a look at the bottom of the page under the heading ‘Add a new cron schedule’. This is where we want to be. Let’s start filling out the form.
- Internal name is just something used by WordPress to keep track of your schedule. Something simple, preferably lower-case with no spaces or weird characters is advised. A good idea for our new schedule would be ‘everyotherday’. Crazy, I know.
- Interval is the number of seconds between executions of the cron job. Instead of trying to figure out the number of seconds in two days, we’ll just let Crontrol do it for us. Enter ‘2 days’ in the box and it will figure out that there are 172800 seconds in two days after the schedule is added. GNU Date Input is what provides this magic and you can use any of the units shown at that link.
- Display name is… wait for it… the name of the schedule that is displayed to the user, you, when selecting schedules. A good value for this might be ‘Weekly’, but ‘wöchentlich’ or ‘toutes les semaines’ work also depending on where you hail from.
- Click on the button at the bottom and gaze in wonder as your new schedule appears in the list. Notice that there’s a delete link to the right of it while next to the other schedules there’s none. This is because you can only delete schedules that have been added by Crontrol to prevent disturbing cron jobs that are expecting certain schedules to be present.
Putting the schedule to use
Now that the schedule is in place, we need to tell WP Database Backup to use it. Luckily, this is super simple. If you go back to the Manage->Backup admin panel, you’ll see that the new ‘everyotherday’ schedule is available. All that’s left to do is select the new schedule and submit the backup job.
Verifying and tweaking
The backup cron job is now added, but let’s go take a look at it to see exactly what and when is scheduled. To do this, go to the Manage->Crontrol admin panel. If there weren’t any hooks present before, there should be one now:
wp_db_backup_cron. You can see that it is using our every other day schedule, as expected. You can also see that it’s scheduled to run about two days from right now. In my case, that means at 11 o’clock in the morning. This isn’t too nice; we’d much rather have it run at 2am or some such time. Here’s how to get that done:
- Click on the ‘Edit’ link next to the
wp_db_backup_croncron job to get the details into the edit box below.
- Change the ‘Next Run’ date to the more desirable 2am time and click on ‘Modify’.
- Verify that the backup job will run next in two days at 2am.
In this post, we’ve exercised two of the main features provided by WP-Crontrol: adding new cron schedules and editing existing cron jobs. Keep an eye on this series on Taking control of WP-Cron using WP-Crontrol to further example of using WP-Crontrol and all of it’s features.
[Edit: If you liked this, you might like some of the posts in my series on Pimping your Manage panel.]