Wordpress Scheduled Tasks

Wordpress has a powerful and surprisingly simple way to schedule tasks. You can use these scheduled tasks to perform maintenance on your site, member management, event management, or any timely functionality you may need.

WordPress Scheduled Tasks
Shawn Wernig
June 19, 2024
by Shawn Wernig

Using the Intervals

First, let’s start by talking about what default intervals are available within WordPress:

  1. hourly
  2. twicedaily
  3. daily

For most people that may be enough – but you can define your own as well; such as weekly, bi-weekly, or monthly. In fact you can create as many interval options as you want, but I’d caution you to keep only the ones you’re actually going to use.

To create additional intervals we need to use the cron_schedules filter as shown here:

add_filter( 'cron_schedules', 'my_custom_intervals' );
function my_custom_intervals( $intervals ) {
// Adds new intervals to the existing intervals.
$intervals['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly' )
);
return $intervals;
}

We need only add a new array item to the $intervals array, then set an interval (in unix time) and a display title.

You can add as many as you like by continuing to add to the $intervals array.

Scheduling an event

To schedule an event we use wp_schedule_event, as shown here:

wp_schedule_event( time(), $interval, $hook, $args);

The function wp_schedule_event takes 3 parameters;

  1. The starting time (in this instance I’ve used the current time)
  2. The $interval, (hourly, daily, twicedaily – or any custom ones you may have set)
  3. The hook id.
  4. The arguments to pass.
Be very careful when scheduling events – ALWAYS check that the event has not already been scheduled to avoid duplicates.

Your code should look something like this, just to be safe:

if (!wp_next_scheduled('my_scheduled_action')) {
wp_schedule_event( time(), 'daily', 'my_scheduled_action');
}

Creating the action

We can now create an action using the $hook parameter, like so:

if (!wp_next_scheduled('my_scheduled_action')) {
wp_schedule_event( time(), 'daily', 'my_scheduled_action');
}

add_action('my_scheduled_action', 'my_scheduled_action_callback');

function my_scheduled_action_callback() {
// do scheduled stuff..
}

Sending arguments

You can send arguments with wp_schedule_event, simply by using the fourth parameter, $args. Send your arguments as an associative array, and they will be sent to your callback function.

This would look like:

if (!wp_next_scheduled('my_scheduled_action')) {
$args = array(
'arg1' => 'foo',
'arg2' => 'bar'
);
wp_schedule_event( time(), 'daily', 'my_scheduled_action', $args);
}

add_action('my_scheduled_action', 'my_scheduled_action_callback');

function my_scheduled_action_callback($arg1, $arg2) {
// do scheduled stuff..
}

Clearing scheduled tasks

It may be important to clear scheduled tasks if they’re no longer needed. To clear a task, we use wp_clear_scheduled_hook and pass it the name of the hook we wish to remove.

wp_clear_scheduled_hook('my_scheduled_action');

Putting it all together

// Create some handy intervals
add_filter( 'cron_schedules', 'my_custom_intervals' );
function my_custom_intervals( $intervals ) {
// Adds new intervals to the existing intervals.
$intervals['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly' )
);
return $intervals;
}

// schedule an event if it's not already scheduled
if (!wp_next_scheduled('my_scheduled_action')) {
wp_schedule_event( time(), 'daily', 'my_scheduled_action');
}

// create an action and callback
add_action('my_scheduled_action', 'my_scheduled_action_callback');
function my_scheduled_action_callback() {
// do scheduled stuff..
}

Important Links

View your current scheduleFFF Cron Manager Plugin

Eggplant Studios - Custom Website Design and Development
Creating custom web solutions and happy clients since 2002
COPYRIGHT ©
2024
Eggplant Studios
- ALL RIGHTS RESERVED
LOGIN