Cron is a standard UNIX utility for running scheduled tasks at a specific time, date, or interval.
However, WordPress Cron is a bit different from the standard utility, and so you need to understand what Cron is in WordPress and how it will work on your site before delving into the actions and process.
What is a WordPress Cron?
WordPress has its own Cron to handle tasks such as checking for updates, scheduling a publication, clearing the shopping cart, and many others. All WordPress Cron tasks are handled through WP-Cron
.
By default, WordPress supports the following time intervals:
- Every hour.
- Twice a day
- Daily
- Weekly
You can add your own interval, and to do this you need to filter cron_schedules
. As an example, let’s add a thirty-second interval:
// Add a filter.
add_filter( 'cron_schedules', 'wpz_cron_30s' );
// Describe the function.
function wpz_cron_30s( $schedules ) {
$schedules['30_seconds'] = array(
'interval' => 30,
'display' => esc_html__( 'Every 30 seconds' ),
);
return $schedules;
}
Managing Cron Tasks in WordPress
To monitor, debug, and manage scheduled actions in WordPress, you can use the command line (WordPress CLI).
And for the vast majority of users and developers, a plugin will suffice WP Crontrol.
Despite the fact that the plugin allows you to create tasks through the interface, we use it only for monitoring and debugging.
Creating Cron tasks in WordPress (WP-Cron)
The most reliable and simple method:
// Describe the deactivation function.
function wpz_deactivate() {
// Remove the 'wpz_cron' task
wp_clear_scheduled_hook( 'wpz_cron' );
}
// Add the main event.
add_action( 'init', function() {
// Create our scheduler event.
add_action( 'wpz_cron', 'wpz_run_cron' );
// Register an event in case of deactivation.
register_deactivation_hook( __FILE__, 'wpz_deactivate' );
// Add our event to WP-Cron.
if ( ! wp_next_scheduled ( 'wpz_cron' ) ) {
wp_schedule_event( time(), '30_seconds', 'wpz_cron' );
}
});
// Describe the function for the scheduler.
function wpz_run_cron() {
// The function will run once every thirty seconds (We added a new, thirty-second interval for WP-Cron above).
}
If the theme in which this code is inserted is activated and there is no scheduled action named wpz_cron, then add wpz_cron
. The wpz_run_cron
function will run every 30 seconds. If the user has changed the theme, then remove wpz_cron
event from the scheduler.
If you think about it, it’s simple enough, and if you’ve installed the WP Control plugin, you’ll see:
One-Time Cron Tasks
In WordPress, you can also add an event that is triggered only once.
// Create the wpz_event event.
add_action( 'wpz_event', 'wpz_in_an_hour', 10, 3 );
// Describe the function for the wpz_event event.
function wpz_in_an_hour( $arg1, $arg2, $arg3 ) {
// Function Description.
}
// Add the wpz_event event to the scheduler (it will run after 1800 seconds from the current time)
wp_schedule_single_event( time() + 1800, 'wpz_event', array( $arg1, $arg2, $arg3 ) );
One-time tasks themselves are rarely used, but it is worth specifying that they are very convenient to use in tasks that are started at a certain interval.