Last active
December 18, 2015 02:59
-
-
Save annalinneajohansson/5715449 to your computer and use it in GitHub Desktop.
Schedule events in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* SCHEDULE AN EVENT TO RUN EVERY HOUR */ | |
/* | |
When you schedule an event in a plugin, | |
call wp_schedule_event on plugin activation otherwise you will end up with a lot of scheduled events. | |
*/ | |
register_activation_hook(__FILE__, 'my_plugin_activation'); | |
function my_plugin_activation() { | |
wp_schedule_event( time(), 'hourly', 'my_hourly_event_action'); | |
} | |
/* Remove the scheduled event on plugin deactivation */ | |
register_deactivation_hook( __FILE__, 'my_plugin_deactivation' ); | |
function my_plugin_deactivation() { | |
wp_clear_scheduled_hook( 'my_hourly_event_action' ); | |
} | |
/* Adding the actual action */ | |
add_action( 'my_hourly_event_action', 'my_hourly_event_do_this' ); | |
function my_hourly_event_do_this() { | |
// Do cool stuff here | |
} | |
/* | |
REGISTER CUSTOM SCHEDULES | |
The default ones are | |
- hourly | |
- twicedaily | |
- daily | |
*/ | |
add_filter( 'cron_schedules', 'add_my_schedules' ); | |
function add_my_schedules( $schedules ) { | |
// Adds once every minute to the existing schedules. | |
$schedules['every_minute'] = array( | |
'interval' => 60, | |
'display' => __( 'Every Minute' ) | |
); | |
return $schedules; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment