WordPress wp_schedule_single_event(): One-Off Background Tasks with WP-Cron

One-off background tasks with wp_schedule_single_event()

wp_schedule_event() creates tasks that repeat forever. Many tasks should run only once, at a certain time: a follow-up email a day after registration, a cleanup an hour after an import, a reminder before an event. wp_schedule_single_event() does that: it runs a hook once at the given time and removes the event from the queue afterwards.

Problem: New users should get a “getting started” email 24 hours after registration, but not if they finish onboarding before that.

Solution: On user_register, schedule a single event with the user ID as the argument, check in the callback whether the email is still needed, and cancel the event with wp_unschedule_event() when onboarding is completed.

Scheduling

<?php
add_action( 'user_register', function ( $user_id ) {
    $args = array( (int) $user_id );

    if ( ! wp_next_scheduled( 'ha_onboarding_followup', $args ) ) {
        wp_schedule_single_event( time() + DAY_IN_SECONDS, 'ha_onboarding_followup', $args );
    }
} );

The time is a UTC timestamp (time(), not current_time( 'timestamp' )). The hook name and the arguments together identify the event: the same hook with another user ID is a separate event. WordPress also refuses to add an identical event within ten minutes of an existing one, which protects against double scheduling.

The callback

<?php
add_action( 'ha_onboarding_followup', function ( $user_id ) {
    $user = get_userdata( $user_id );

    if ( ! $user || get_user_meta( $user_id, 'ha_onboarding_done', true ) ) {
        return; // user deleted or already onboarded
    }

    wp_mail(
        $user->user_email,
        __( 'Getting started', 'textdomain' ),
        sprintf( __( "Hi %s,\n\nhere are a few tips for your first steps...", 'textdomain' ), $user->display_name )
    );
} );

Check the conditions again when the task runs: a day is a long time, and the user may have been deleted or have finished onboarding in the meantime.

Cancelling

<?php
function ha_complete_onboarding( $user_id ) {
    update_user_meta( $user_id, 'ha_onboarding_done', 1 );

    $args = array( (int) $user_id );
    $time = wp_next_scheduled( 'ha_onboarding_followup', $args );

    if ( $time ) {
        wp_unschedule_event( $time, 'ha_onboarding_followup', $args );
    }
}

wp_clear_scheduled_hook( 'ha_onboarding_followup', $args ) removes all events of the hook with these arguments at once. Pass exactly the same arguments as when scheduling: array( 5 ) and array( '5' ) are different events.

When does it really run

WP-Cron is triggered by page views: an event is due at its time, but it runs with the first request after that. On a quiet site “in 24 hours” can mean 26 hours. If timing matters, disable the page-view trigger and run cron from the server every minute or five:

// wp-config.php
define( 'DISABLE_WP_CRON', true );
*/5 * * * * cd /var/www/html && wp cron event run --due-now >/dev/null 2>&1

Due events are visible with wp cron event list, which is the quickest way to check that the scheduling works.

NOTE: Keep the arguments small and simple (IDs, not objects): they are stored serialised in the cron option, which is loaded on every request. For thousands of scheduled tasks, a queue with its own table such as Action Scheduler (used by WooCommerce) scales better than WP-Cron.