Using WordPress ‘cron_schedules’ PHP filter

The cron_schedules WordPress PHP filter allows you to add or modify non-default cron schedules in WordPress.

Usage

add_filter('cron_schedules', 'your_custom_cron_schedules_function');
function your_custom_cron_schedules_function($schedules) {
    // your custom code here
    return $schedules;
}

Parameters

  • $schedules (array): An array of non-default cron schedules keyed by the schedule name. Contains two keys: interval and display.

More information

See WordPress Developer Resources: cron_schedules

Examples

Add a custom cron schedule

Add a custom cron schedule that runs every 5 minutes:

add_filter('cron_schedules', 'add_five_minutes_schedule');
function add_five_minutes_schedule($schedules) {
    $schedules['every_five_minutes'] = array(
        'interval' => 300, // 5 * 60 seconds
        'display' => __('Every 5 Minutes')
    );
    return $schedules;
}

Add a custom cron schedule for daily tasks

Add a custom cron schedule that runs every day at a specific time:

add_filter('cron_schedules', 'add_daily_schedule');
function add_daily_schedule($schedules) {
    $schedules['daily_at_specific_time'] = array(
        'interval' => 86400, // 24 * 60 * 60 seconds
        'display' => __('Daily at a Specific Time')
    );
    return $schedules;
}

Add multiple custom cron schedules

Add multiple custom cron schedules at once:

add_filter('cron_schedules', 'add_multiple_custom_schedules');
function add_multiple_custom_schedules($schedules) {
    $schedules['every_three_hours'] = array(
        'interval' => 10800, // 3 * 60 * 60 seconds
        'display' => __('Every 3 Hours')
    );

    $schedules['every_six_hours'] = array(
        'interval' => 21600, // 6 * 60 * 60 seconds
        'display' => __('Every 6 Hours')
    );

    return $schedules;
}

Modify an existing cron schedule

Modify the default hourly cron schedule to run every 30 minutes instead:

add_filter('cron_schedules', 'modify_hourly_schedule');
function modify_hourly_schedule($schedules) {
    if (isset($schedules['hourly'])) {
        $schedules['hourly']['interval'] = 1800; // 30 * 60 seconds
        $schedules['hourly']['display'] = __('Every 30 Minutes');
    }
    return $schedules;
}

Remove a custom cron schedule

Remove a custom cron schedule by its name:

add_filter('cron_schedules', 'remove_custom_schedule');
function remove_custom_schedule($schedules) {
    unset($schedules['custom_schedule_name']);
    return $schedules;
}