Cron Schedules and Events with Examples

Advertisement

In this article we are going to see how to use WordPress Cron or WP Cron to register custom cron schedules and cron events.

You are going to learn:

See all WordPress Cron schedules? See all WordPress Cron schedules?

We can use the function wp_get_schedules() which show all the exiting schedules something like below:

Top ↑

Add WordPress Cron Schedules Add WordPress Cron Schedules

In WordPress we have a filer cron_schedules which allow us to add custom schedule.

We have see we can use the function wp_get_schedules() to see all the existing schedules.

In above example we have below schedules:

  • hourly
  • twicedaily
  • daily
  • weekly

But, We can use the cron_schedules to add our own custom schedules. Let’s add a every_ten_minutes schedule to perform some action.

<?php
/**
* Add Cron Schedules
*
* @todo Change the `prefix_` and with your own unique prefix.
*
* @since 1.0.0
*
* @param array() $schedules Existing schedules.
* @return array
*/
if( ! function_exists( 'prefix_add_cron_schedules' ) ) :
function prefix_add_cron_schedules( $schedules = array() ) {
$schedules['every_ten_minutes'] = array(
'interval' => 600, // 600 seconds means 10 minutes.
'display' => __( 'Every 10 minutes', 'textdomain' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'prefix_add_cron_schedules' );
endif;

Here, We have added a new schedule with 600 seconds interval means for every 10 minutes.

Top ↑

Add WordPress Cron Event Add WordPress Cron Event

We have register the custom schedule every_ten_minutes for every 10 minutes.

Now, Lets add the a custom schedule event or cron event.

We can register the schedule event with function wp_schedule_event().

See below code snippet:

<?php
/**
* Add schedule event or Cron event with wp_schedule_event()
*
* @todo Change the `prefix_` and with your own unique prefix.
*
* @since 1.0.0
*
* @return void
*/
if( ! function_exists( 'prefix_add_scheduled_event' ) ) :
function prefix_add_scheduled_event() {
// Schedule the event if it is not scheduled.
if ( ! wp_next_scheduled( 'prefix_cron_hook' ) ) {
wp_schedule_event( time(), 'every_ten_minutes', 'prefix_cron_hook' );
}
}
add_action( 'admin_init', 'prefix_add_scheduled_event' );
endif;

Here, We have register the prefix_cron_hook schedule event. This scheduled event trigger for every 10 minutes.

But, We have to add a action which is same as our scheduled event.

We can use the same action prefix_cron_hook to perform some task. So, Lets add a sample task.

Top ↑

Add WordPress Cron Task Add WordPress Cron Task

We can use the function add_action() to perform our scheduled event with hook prefix_cron_hook.

See below code for reference:

<?php
/**
* Task to perform
*
* @todo Change the `prefix_` and with your own unique prefix.
*
* @since 1.0.0
*
* @return void
*/
if( ! function_exists( 'prefix_cron_task' ) ) :
function prefix_cron_task() {
error_log( 'Called' );
}
add_action( 'prefix_cron_hook', 'prefix_cron_task' );
endif;

Here, We have used function error_log() into our scheduled event task. So, Wherever our event prefix_cron_hook event trigger (every 10 minites) then our hook prefix_cron_hook called.

And our function prefix_cron_task() called which is hooked in action prefix_cron_hook.

In short; the string Called is added into the debug.log file (if WP_DEBUG & WP_DEBUG_LOG is true from wp-config.php file)

Top ↑

Test WordPress Cron Events? Test WordPress Cron Events?

We have an amazing WordPress plugin called WP Crontrol. The plugin WP Crontrol provide a user interface in which we can see the all registered scheduled and events.

Install and Activate the WP Crontrol plugin from WordPress back-end (wp-admin) Install and Activate the WP Crontrol plugin from WordPress back-end (wp-admin)

Follow below steps to install the WP Crontrol plugin.

  • Go to Plugins > Add new
  • Search for WP Crontrol
  • Click on the Install now and then Activate button

See below screenshot for reference:

Install and Activate plugin WP Crontrol to debug WordPress Cron schedules and events
Install and Activate plugin WP Crontrol

Top ↑

Install and Activate WP Crontrol with WP CLI Install and Activate WP Crontrol with WP CLI

Alternatively, If you have a WP CLI setup then you can install the WP Crontrol plugin with WP CLI command.

Use below command which install and activate the WP Crontrol plugin.

wp plugin install wp-crontrol --activate

If installed but not activate then use blow command to activate the WP Crontrol with command line

wp plugin activate wp-crontrol

Top ↑

See all WordPress Cron Schedules? See all WordPress Cron Schedules?

To see all the existing Cron schedules navigate to to Settings > Cron Schedules.

See below screenshot for reference:

See all the WordPress Cron Schedules with the help of WP Crontrol plugin.
See all Cron Schedules

Here, You can see our registered schedule every_ten_minutes which indicate the interval 600 seconds (Every 10 minutes).

Top ↑

See all WordPress Cron Events See all WordPress Cron Events

To see all cron event navigate to Tools > Cron Events.

See below screenshot for reference:

See all the WordPress Cron Events with the help of WP Crontrol plugin.
See all Cron Events

Here, You can see our scheduled event prefix_cron_hook which is recurrence (trigger for every) 10 minutes.

Top ↑

Manually Run the WordPress Cron Event Manually Run the WordPress Cron Event

Yes, The plugin WP Crontrol allow us to execute the Cron Event manually.

Just click on Run Now action link which appear blow the scheduled event on hover.

See below screenshot for reference:

Manually run the WordPress Cron event with the help of WP Crontrol plugin.
Manually Run the Cron Event

Now, On click on Run now to and see the debug.log file which show you the string Called. You can see something below string:

[02-Jun-2020 13:57:08 UTC] Called

See below screenshot for reference:

Cron Schedules and Events with Examples 1
Cron Event Log

Here, For testing I have just used code error_log( ‘Called’ );. You can use your own task.

NOTE: In the debug.log file we cant see the logging data until we enable the debug log. We can use set constant DEBUG_LOG to true to enable the debug log.

Leave a Reply