Using WordPress ‘pre_reschedule_event’ PHP filter

The ‘pre_reschedule_event’ filter allows you to modify or hijack the rescheduling process of a recurring event in WordPress. If a non-null value is returned, the normal rescheduling process will be bypassed and the filtered value will be used instead.

Usage

add_filter('pre_reschedule_event', 'your_function_name', 10, 3);

function your_function_name($pre, $event, $wp_error) {
    // Your custom code here
    return $pre;
}

Parameters

  • $pre: null|bool|WP_Error – The value to return instead. Default is null, which continues the normal event rescheduling process.
  • $event: object – An object containing the event’s data.
  • $wp_error: bool – Determines if a WP_Error should be returned on failure.

Examples

Disable event rescheduling for a specific hook

add_filter('pre_reschedule_event', 'disable_specific_hook_reschedule', 10, 3);

function disable_specific_hook_reschedule($pre, $event, $wp_error) {
    if ($event->hook == 'my_custom_hook') {
        return false;
    }
    return $pre;
}

This code disables the rescheduling of an event with the hook ‘my_custom_hook’ by returning false.

Reschedule event to a different time

add_filter('pre_reschedule_event', 'reschedule_event_different_time', 10, 3);

function reschedule_event_different_time($pre, $event, $wp_error) {
    if ($event->hook == 'my_custom_hook') {
        $event->timestamp += 3600; // Add 1 hour
        return true;
    }
    return $pre;
}

This example changes the reschedule time for events with the hook ‘my_custom_hook’ by adding an extra hour to their timestamp.

Log event rescheduling

add_filter('pre_reschedule_event', 'log_event_rescheduling', 10, 3);

function log_event_rescheduling($pre, $event, $wp_error) {
    error_log("Event '{$event->hook}' is being rescheduled.");
    return $pre;
}

This code logs a message in the error log whenever an event is rescheduled.

Prevent event rescheduling on weekends

add_filter('pre_reschedule_event', 'prevent_weekend_reschedule', 10, 3);

function prevent_weekend_reschedule($pre, $event, $wp_error) {
    $weekday = date('w', $event->timestamp);
    if ($weekday == 0 || $weekday == 6) { // 0 for Sunday, 6 for Saturday
        return false;
    }
    return $pre;
}

This example prevents events from being rescheduled on weekends by returning false if the event’s timestamp falls on a Saturday or Sunday.

Set a maximum number of event occurrences

add_filter('pre_reschedule_event', 'limit_event_occurrences', 10, 3);

function limit_event_occurrences($pre, $event, $wp_error) {
    $max_occurrences = 10;
    if ($event->hook == 'my_custom_hook') {
        $occurrences = (int) get_option('my_custom_hook_occurrences', 0);
        if ($occurrences >= $max_occurrences) {
            return false;
        }
        update_option('my_custom_hook_occurrences', ++$occurrences);
    }
    return $pre;
}

This code limits the number of occurrences for events with the hook ‘my_custom_hook’ to a maximum of 10. It does this by storing and updating the occurrences count in a WordPress option. If the event has already occurred 10 times or more, it will not be rescheduled.