How To Schedule Tasks In WordPress With Wp-Cron
WP-Cron runs tasks on a schedule, like cleaning old data, syncing an API or sending a report. It does not use a real server cron. WordPress checks for due tasks when someone visits the site and runs them in the background.
Schedule a daily task
function lotgrafix_schedule_event() {
if ( ! wp_next_scheduled( 'lotgrafix_daily_task' ) ) {
wp_schedule_event( time(), 'daily', 'lotgrafix_daily_task' );
}
}
add_action( 'init', 'lotgrafix_schedule_event' );
function lotgrafix_do_daily_task() {
// Your code here, for example delete old transients or sync data.
update_option( 'lotgrafix_last_run', current_time( 'mysql' ) );
}
add_action( 'lotgrafix_daily_task', 'lotgrafix_do_daily_task' );
The wp_next_scheduled() check prevents scheduling the same task again on every page load.
Built in intervals
hourlytwicedailydailyweekly
Create a custom interval
function lotgrafix_cron_schedules( $schedules ) {
$schedules['lotgrafix_every_fifteen'] = array(
'interval' => 15 * MINUTE_IN_SECONDS,
'display' => __( 'Every 15 Minutes', 'lotgrafix' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'lotgrafix_cron_schedules' );
Then use 'lotgrafix_every_fifteen' as the recurrence in wp_schedule_event().
Run a task only once
wp_schedule_single_event( time() + 60, 'lotgrafix_send_followup', array( $user_id ) );
function lotgrafix_send_followup( $user_id ) {
// Runs about one minute later.
}
add_action( 'lotgrafix_send_followup', 'lotgrafix_send_followup' );
Remove the task when a plugin is deactivated
function lotgrafix_unschedule_event() {
$timestamp = wp_next_scheduled( 'lotgrafix_daily_task' );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, 'lotgrafix_daily_task' );
}
}
register_deactivation_hook( __FILE__, 'lotgrafix_unschedule_event' );
Use a real server cron for reliability
On low traffic sites WP-Cron can run late, and on high traffic sites it adds extra requests. Disable it in wp-config.php and call it from the server.
define( 'DISABLE_WP_CRON', true );
*/5 * * * * curl -s https://example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
Tip
The free WP Crontrol plugin shows all scheduled events and lets you run them manually while testing.


