Another WordPress Cron Example
Here is another WP Cron example for my previous article on WP Cron tips.
In this example, we’re setting a custom cron schedule and using it to schedule and execute our custom function every three minutes.
// add custom time to cron
add_filter('cron_schedules', 'filter_cron_schedules');
function filter_cron_schedules($param) {
return array('three_minutes' => array(
'interval' => 180, // seconds
'display' => __('Every 3 minutes')
));
}
// cron for caching counts
add_action('wp', 'sfs_cron_activation');
function sfs_cron_activation() {
if (!wp_next_scheduled('sfs_cron_cache')) {
wp_schedule_event(time(), 'three_minutes', 'sfs_cron_cache'); // hourly, daily, twicedaily, three_minutes
}
}
// cache feed counts
add_action('sfs_cron_cache', 'sfs_cache_data');
function sfs_cache_data() {
// cache feed counts (for example)
// this function will run every three minutes
// add whatever functionality is required
}
Note that the sfs_cache_data()
function is gutted for clarity; in it, you can add whatever functionality that needs run every three minutes. To change the custom interval to something other than three minutes, edit the interval
value (in seconds) and display
value accordingly.