Set up a Cron Job: Using WP-CRON

WordPress's Cron system is a tool that you can use to schedule automated tasks.

Wouldn’t it be great if you could set up a schedule to run tasks at a specific time in the future? Thankfully, there is a tool that can do just that. Unfortunately, the WordPress documentation does not go into much detail, so we will explain how to use and setup a cron job on your server using wp-cron. Before we begin, let’s take a look at what a cron job actually is. You can add WP-Cron to your plugins to get things done fast and automatic.

What is a Cron Job?

Cron is a task-running scheduler utility that is made for UNIX-based operating systems. Basically, you can set your server to run a task at a certain time and it will run the task automatically, without your intervention. This sounds great, however, you can run into some problems when trying to get a cron job to work. Let’s take a look at some of the issues.

Issues

These are only a few of the difficulties that arise when you are trying to set a cron job on your server:

  1. For security reasons, your hosting provider may not allow you to run cron jobs
  2. Different servers have different cron systems set up

On top of that, WordPress Cron is different than a UNIX cron job. For example, in WordPress cron, if you do not have any visitors, your cron job might not run (There is a workaround to this pitfall, please continue reading to find out). Remember to keep cron jobs as simple and minimal as you can! If you have too many scheduled events or you have heavy actions ,it will slow down your server. What are WordPress Cron jobs used for? What tasks do they run?

Examples

  • Backup your WordPress site and database
  • Optimize database
  • Checking WordPress version
  • A post scheduled to be published at a specific time
  • Send out emails for marketing purposes

Schedule an Event

Remember the ‘no visitor’ pitfall that was discussed earlier? Thankfully, there is a workaround. You will have to write In order to accomplish this; setting up a WordPress cron job requires you to have some programming/PHP knowledge. The first thing that you will need to do is use the wp_schedule_event function. The wp_schedule_event function has four parameters:  $timestamp, $recurrence, $hook and $args.

  1. $timestamp: This parameter takes a UNIX-based timestamp and must be in GMT time, you can use the time() variable (required)
  2. $occurence: The amount of times that the WordPress cron job should run: daily, hourly… (required)
  3. $hooks: Any custom WordPress hooks for the cron job to look for  (required)
  4. $args: Any extra arguments that the cron job will take (optional)

Let’s take  a look at an example on the WordPress Codex found here. The following code will run a task hourly. You should place the code in a custom plugin and make sure to check for activation, or it could turn out messy:

register_activation_hook( __FILE__, 'prefix_activation' );
/**
 * On activation, set a time, frequency and name of an action hook to be scheduled.
 */
function prefix_activation() {
	wp_schedule_event( time(), 'hourly', 'prefix_hourly_event_hook' );
}

add_action( 'prefix_hourly_event_hook', 'prefix_do_this_hourly' );
/**
 * On the scheduled action hook, run the function.
 */
function prefix_do_this_hourly() {
	// do something every hour
}

Scheduling a Single Event

Unlike the previous example, if you would like to set a single Cron job, then you will need to use the wp_schedule_single_event WordPress function:

function do_this_in_an_hour() {

    // do something
}
add_action( 'my_new_event','do_this_in_an_hour' );

// put this line inside a function, 
// presumably in response to something the user does
// otherwise it will schedule a new event on every page visit

wp_schedule_single_event( time() + 3600, 'my_new_event' );

// time() + 3600 = one hour from now.

Removing an Event

You can remove an event by using the wp_clear_scheduled_hook function. You should create a check to make sure that this happens on deactivation to be safe. The code written below can be found on the WordPress Codex.

// If you previously added for example
// wp_schedule_single_event( time() + 3600, 'my_new_event' );

wp_clear_scheduled_hook( 'my_new_event' );

// or this if you created something like
// wp_schedule_single_event( time() + 3600, 'my_new_event', array( 'some_arg' ) );

wp_clear_scheduled_hook( 'my_new_event', array( 'some_arg' ) );

Videos

If you happen to be confused, you can watch a video tutorial by Jay Docherty:

Plugins

Alternatively, there are two plugins to view and control WP-Cron and the scheduled tasks within:

WP Control Plugin

WP-Cron Control Plugin

EasyCron Plugin
Note: The EasyCron plugin is a bit dated. Use at your own risk. This plugin makes it very easy to install and use WP Cron which will run WordPress’s cron scripts periodically.

Alternatives

Finally, there is another way to set cron jobs. But, it requires you to disable WordPress Cron by adding the following to your wp-cron.php file:

define(‘DISABLE_WP_CRON’, true);

Next, you will need to upload the following to your server. You will then need to log into your cPanel via your hosting provider and use the cron utility (At times, this is hidden in the ‘Advanced’ panel). You can find more detailed instructions here. Otherwise, you can watch a video about it:

Sources

Cron – Wikipedia article

Smashing Magazine – Schedule Events Using WordPress Cron

OUR SERVICE

Fan Commerce

ファンコマース

ファンとのダイレクトなコミュニケーションを実現して「感動体験」をビジネスに結びつけます。

Digital Marketing

デジタルマーケティング

未来を創造する価値ある「顧客体験」を実現する仕組みづくりを提案。