WP-Cron in depth: reliable scheduled tasks in WordPress
WP-Cron is a pseudo-cron: there is no daemon. On page loads WordPress checks the list of scheduled events (the cron option), and if one is due, it sends a non-blocking request to wp-cron.php, which runs the events in the background. On a site with constant traffic this works: an event due at 14:00 runs with the first visit after 14:00. On a low-traffic site (staging, an intranet, an API backend) events can be hours late, and on a fully cached site the cached pages never run PHP, so they never trigger cron at all.
Problem: A daily import from an external API should run at 03:00, but on a staging site with no night visitors it runs hours late. On busy mornings the same site has response-time spikes from the cron spawn requests.
Solution: Disable the page-load trigger with DISABLE_WP_CRON, run due events from a real system cron job with WP-CLI every minute, and schedule the events in the site’s time zone.
Run cron from the system
In wp-config.php:
<?php define( 'DISABLE_WP_CRON', true );
Then a crontab entry for the user that owns the site files (sudo crontab -u www-data -e on Debian/Ubuntu):
* * * * * /usr/local/bin/wp --path=/var/www/example.com cron event run --due-now --quiet >> /var/log/wp-cron.log 2>&1
Every minute WP-CLI runs exactly the events that are due, independent of visitors and of the page cache. Without WP-CLI, request the cron script instead: * * * * * curl -s https://example.com/wp-cron.php?doing_wp_cron > /dev/null.
With DISABLE_WP_CRON and no system job, all scheduled events stop, including core ones: update checks, the cleanup of expired transients, scheduled posts (“Missed schedule”).
Custom intervals
WordPress has hourly, twicedaily, daily and weekly. Others are added with a filter:
<?php
add_filter( 'cron_schedules', function ( $schedules ) {
$schedules['every_fifteen_minutes'] = array(
'interval' => 15 * MINUTE_IN_SECONDS,
'display' => __( 'Every 15 minutes', 'myplugin' ),
);
return $schedules;
} );
Schedule the events
<?php
register_activation_hook( __FILE__, function () {
if ( ! wp_next_scheduled( 'myplugin_daily_import' ) ) {
// 03:00 in the site's time zone (Settings → General), not the server's
$first_run = new DateTime( 'tomorrow 03:00', wp_timezone() );
wp_schedule_event( $first_run->getTimestamp(), 'daily', 'myplugin_daily_import' );
}
if ( ! wp_next_scheduled( 'myplugin_cache_warmup' ) ) {
wp_schedule_event( time(), 'every_fifteen_minutes', 'myplugin_cache_warmup' );
}
} );
register_deactivation_hook( __FILE__, function () {
wp_clear_scheduled_hook( 'myplugin_daily_import' );
wp_clear_scheduled_hook( 'myplugin_cache_warmup' );
} );
Event timestamps are always UTC. strtotime( 'tomorrow 03:00' ) would mean 03:00 UTC, because WordPress sets PHP’s default time zone to UTC; wp_timezone() gives the zone from the settings. A custom interval used on activation must be registered before (the filter above is in the main plugin file, so it is).
The callbacks
<?php
add_action( 'myplugin_daily_import', function () {
$response = wp_remote_get( 'https://api.example.com/products', array(
'timeout' => 60,
'headers' => array( 'Authorization' => 'Bearer ' . get_option( 'myplugin_api_key' ) ),
) );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
error_log( 'myplugin_daily_import failed: ' . ( is_wp_error( $response ) ? $response->get_error_message() : wp_remote_retrieve_response_code( $response ) ) );
return;
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $data ) ) {
return;
}
update_option( 'myplugin_last_import_data', $data, false ); // not autoloaded
update_option( 'myplugin_last_import_time', time(), false );
} );
add_action( 'myplugin_cache_warmup', function () {
delete_transient( 'myplugin_homepage_data' );
myplugin_get_homepage_data(); // rebuilds and stores the transient
} );
A cron callback runs without a logged-in user and without a request context: do not rely on is_admin(), the current user or $_GET there. WP-Cron has no retries; for jobs that must be retried on failure or are many (thousands of items), a queue such as Action Scheduler (bundled with WooCommerce) is the better tool. Caching inside such tasks is covered in the Transients API.
Inspect and debug
wp cron event list --fields=hook,next_run_relative,recurrence wp cron event run myplugin_daily_import # run one now, ignoring the schedule wp cron event delete myplugin_cache_warmup wp cron test # checks that the HTTP spawn works
Events whose next_run_relative is in the past (“1 hour ago”) mean that nothing is running cron. More WP-CLI commands are in essential WP-CLI commands.
NOTE: On shared hosting without access to cron, leave DISABLE_WP_CRON off, or use the hosting panel’s cron feature or an external cron service to request wp-cron.php every few minutes. Do not schedule events on init without the wp_next_scheduled() check: every page load would add another copy.