Skip to content

Instantly share code, notes, and snippets.

@karlazz
Created January 19, 2013 07:04
Show Gist options
  • Save karlazz/4571160 to your computer and use it in GitHub Desktop.
Save karlazz/4571160 to your computer and use it in GitHub Desktop.
scheduling a task to test wp-cron
if (!wp_next_scheduled('my_task_hook')) {
wp_schedule_event( time(), 'hourly', 'my_task_hook' );
}
add_action( 'my_task_hook', 'my_task_function' );
function my_task_function() {
wp_mail('you@yoursite.com', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');
}
And to delete the item
delete_action( 'my_task_hook', 'my_task_deactivate' );
// clean the scheduler
function my_task_deactivate() {
wp_clear_scheduled_hook( 'my_task_hook' );
}
But not always are 3 time values enough. Luckily you can expand the control via a filter.
add_filter( 'cron_schedules', 'filter_cron_schedules' );
// add custom time to cron
function filter_cron_schedules( $param ) {
return array( 'once_half_hour' => array(
'interval' => 1800, // seconds
'display' => __( 'Once Half an Hour' )
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment