Using WordPress ‘cron_unschedule_event_error’ PHP action

The cron_unschedule_event_error WordPress PHP action fires when an error occurs while unscheduling a cron event.

Usage

add_action('cron_unschedule_event_error', 'your_custom_function', 10, 3);

function your_custom_function($result, $hook, $v) {
    // Your custom code here

    return $result;
}

Parameters

  • $result (WP_Error) – The WP_Error object.
  • $hook (string) – Action hook to execute when the event is run.
  • $v (array) – Event data.

More information

See WordPress Developer Resources: cron_unschedule_event_error

Examples

Log the error message

Log the error message when an unscheduling error occurs.

add_action('cron_unschedule_event_error', 'log_unschedule_error', 10, 3);

function log_unschedule_error($result, $hook, $v) {
    error_log("Error unscheduling {$hook}: " . $result->get_error_message());
    return $result;
}

Send an email notification

Send an email notification to the admin when an error occurs unscheduling an event.

add_action('cron_unschedule_event_error', 'email_unschedule_error', 10, 3);

function email_unschedule_error($result, $hook, $v) {
    $admin_email = get_option('admin_email');
    $subject = 'Cron Unschedule Error';
    $message = "Error unscheduling {$hook}: " . $result->get_error_message();

    wp_mail($admin_email, $subject, $message);
    return $result;
}

Retry unscheduling the event

Retry unscheduling the event when an error occurs.

add_action('cron_unschedule_event_error', 'retry_unschedule_event', 10, 3);

function retry_unschedule_event($result, $hook, $v) {
    if (wp_next_scheduled($hook, $v)) {
        wp_unschedule_event(wp_next_scheduled($hook, $v), $hook, $v);
    }
    return $result;
}

Store unscheduling errors in a custom database table

Store unscheduling errors in a custom database table for further analysis.

add_action('cron_unschedule_event_error', 'store_unschedule_error', 10, 3);

function store_unschedule_error($result, $hook, $v) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'unschedule_errors';

    $wpdb->insert(
        $table_name,
        array(
            'hook' => $hook,
            'error_message' => $result->get_error_message(),
            'event_data' => json_encode($v),
            'timestamp' => current_time('mysql'),
        )
    );

    return $result;
}

Add a custom notice in the admin dashboard

Display a custom notice in the admin dashboard when an unscheduling error occurs.

add_action('cron_unschedule_event_error', 'add_unschedule_error_notice', 10, 3);

function add_unschedule_error_notice($result, $hook, $v) {
    set_transient('unschedule_error_notice', $result->get_error_message(), 60);

    add_action('admin_notices', 'display_unschedule_error_notice');
    return $result;
}

function display_unschedule_error_notice() {
    $error_message = get_transient('unschedule_error_notice');

    if ($error_message) {
?>
<div class="notice notice-error">
<p><?php echo "Error unscheduling cron event: " . esc_html($error_message); ?></p>
</div>
<?php
delete_transient('unschedule_error_notice');
}
}