Knowing how to use WordPress Crons is incredibly useful for scheduling automated actions, but is also difficult to learn how to implement. This article will attempt to explain how WordPress Crons work and how to schedule a Cron inside your plugin. WordPress provides documentation on how to use WP-Cron but it can feel overwhelming to dig through documentation. Fortunately, it’s easy to use WordPress Crons once you understand how they work.
How does a WP-Cron work?
Before writing out example code for creating a WordPress Cron it is important to understand how they work. Unlike conventional server Crons that run at exact times, WordPress Crons are not exact and may run long after they are scheduled. This delay is because WordPress Crons are triggered by HTTP requests. The more people who visit your website, the more accurate your Crons will be. A simple explanation WordPress Crons is that when you schedule a Cron WordPress stores 3 pieces of information.- A unique label
- A time interval
- The time that the Cron should be run next
How to schedule a basic WP-Cron
First we need to give our Cron a unique label.global $IBH_CRON_HOOK; // Tell WordPress that this will be used as a global variable $IBH_CRON_HOOK = 'ibh_cron_hook'; // Initialize the unique label of our WP-CRON hookSecond, we need need to set the interval to tell WordPress how often our Cron should run.
add_filter('cron_schedules', 'IBH_CronInterval'); // Add a custom WP-CRON interval function IBH_CronInterval($schedules) { // Custom WP-CRON interval $schedules['fifteen_minutes'] = array( 'interval' => 900, // Seconds 'display' => esc_html__('Every 15 Minutes'), ); return $schedules; }Third, we need to activate the Cron. A good place to activate a Cron is when your plugin is first activated.
register_activation_hook(__FILE__, 'IBH_CronActivate'); // Register the CRON when the plugin is activated function IBH_CronActivate() { global $IBH_CRON_HOOK; if (!wp_next_scheduled($IBH_CRON_HOOK)) { // If the cron is NOT scheduled... wp_schedule_event(time(), 'fifteen_minutes', $IBH_CRON_HOOK); // Schedule the cron } }Next, we need to tell WordPress what function to call when our Cron is run by creating a hook.
add_action($IBH_CRON_HOOK, 'IBH_CronJob'); // Create the WP_CRON hook function IBH_CronJob() { // The function we want to run // Here is where you call functions that need to be run every 15 minutes (or however often your Cron runs) }Finally, if your plugin is deactivated or no longer needs to be run it is always a good idea to clean up after yourself. To clean up after yourself you simply unregister your Cron. You can unregister it at any time but unregistering the Cron when the plugin is deactivated is always a good time to do final housekeeping.
register_deactivation_hook( __FILE__, 'IBH_CronDeactivate'); // Unregister the CRON on plugin deactivation function IBH_CronDeactivate() { // Unregister the CRON to free up resources global $IBH_CRON_HOOK; $timestamp = wp_next_scheduled($IBH_CRON_HOOK); wp_unschedule_event($timestamp, $IBH_CRON_HOOK); }That’s all there is to it! It is surprisingly simple to register a WordPress Cron and unregister it once you are done with it. Let’s put it all together!
global $IBH_CRON_HOOK; // Tell WordPress that this will be used as a global variable $IBH_CRON_HOOK = 'ibh_cron_hook'; // Initialize the unique label of our WP-CRON hook add_filter('cron_schedules', 'IBH_CronInterval'); // Add a custom WP-CRON interval register_activation_hook(__FILE__, 'IBH_CronActivate'); // Register the CRON when the plugin is activated add_action($IBH_CRON_HOOK, 'IBH_CronJob'); // Create the WP_CRON hook register_deactivation_hook( __FILE__, 'IBH_CronDeactivate'); // Unregister the CRON on plugin deactivation function IBH_CronInterval($schedules) { // Custom WP-CRON interval $schedules['fifteen_minutes'] = array( 'interval' => 900, // Seconds 'display' => esc_html__('Every 15 Minutes'), ); return $schedules; } function IBH_CronActivate() { global $IBH_CRON_HOOK; if (!wp_next_scheduled($IBH_CRON_HOOK)) { // If the cron is NOT scheduled... wp_schedule_event(time(), 'fifteen_minutes', $IBH_CRON_HOOK); // Schedule the cron } } function IBH_CronJob() { // The function we want to run // Here is where you call functions that need to be run every 15 minutes (or however often your Cron runs) } function IBH_CronDeactivate() { // Unregister the CRON to free up resources global $IBH_CRON_HOOK; $timestamp = wp_next_scheduled($IBH_CRON_HOOK); wp_unschedule_event($timestamp, $IBH_CRON_HOOK); }