Using WordPress ‘pre_schedule_event’ PHP filter

The ‘pre_schedule_event’ WordPress PHP filter schedules a recurring event by attaching a hook that gets triggered by WordPress at the specified interval.

Table of contents

The action occurs when someone visits your WordPress site if the scheduled time has passed.

Usage

To schedule a recurring event, use the wp_schedule_event() function with the following parameters:

wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false );

Parameters

  • $timestamp (int): Required. Unix timestamp (UTC) for when to next run the event.
  • $recurrence (string): Required. How often the event should subsequently recur. See wp_get_schedules() for accepted values.
  • $hook (string): Required. Action hook to execute when the event is run.
  • $args (array): Optional. Array containing arguments to pass to the hook’s callback function. Each value in the array is passed to the callback as an individual parameter. The array keys are ignored. Default: array()
  • $wp_error (bool): Optional. Whether to return a WP_Error on failure. Default: false

Examples

Schedule a daily backup

In this scenario, we want to schedule a daily backup of our WordPress site.

function my_daily_backup() {
    // Your backup logic here
}

add_action('my_daily_backup_hook', 'my_daily_backup');

if (!wp_next_scheduled('my_daily_backup_hook')) {
    wp_schedule_event(time(), 'daily', 'my_daily_backup_hook');
}

This code schedules a daily backup by adding an action named my_daily_backup_hook. It first checks if the event is already scheduled using wp_next_scheduled(). If not, it schedules the event using wp_schedule_event() with a ‘daily’ recurrence.

Schedule an hourly email notification

In this scenario, we want to send an email notification to the administrator every hour.

function hourly_email_notification() {
    // Your email sending logic here
}

add_action('hourly_email_notification_hook', 'hourly_email_notification');

if (!wp_next_scheduled('hourly_email_notification_hook')) {
    wp_schedule_event(time(), 'hourly', 'hourly_email_notification_hook');
}

This code schedules an hourly email notification by adding an action named hourly_email_notification_hook. It first checks if the event is already scheduled using wp_next_scheduled(). If not, it schedules the event using wp_schedule_event() with an ‘hourly’ recurrence.

Schedule a twice daily database optimization

In this scenario, we want to optimize our WordPress database twice daily.

function optimize_database() {
    // Your database optimization logic here
}

add_action('optimize_database_hook', 'optimize_database');

if (!wp_next_scheduled('optimize_database_hook')) {
    wp_schedule_event(time(), 'twicedaily', 'optimize_database_hook');
}

This code schedules a twice daily database optimization by adding an action named optimize_database_hook. It first checks if the event is already scheduled using wp_next_scheduled(). If not, it schedules the event using wp_schedule_event() with a ‘twicedaily’ recurrence.

Schedule a custom interval event

In this scenario, we want to schedule an event to run every 30 minutes.

function add_custom_cron_schedule($schedules) {
    $schedules['every_30_minutes'] = array(
        'interval' => 30 * MINUTE_IN_SECONDS,
        'display' => 'Every 30 minutes'
    );
    return $schedules;
}
add_filter('cron_schedules', 'add_custom_cron_schedule');
function run_every_30_minutes() { // Your logic for the event here }
add_action('run_every_30_minutes_hook', 'run_every_30_minutes');
if (!wp_next_scheduled('run_every_30_minutes_hook')) { 
wp_schedule_event(time(), 'every_30_minutes', 'run_every_30_minutes_hook'); 
}

This code schedules an event to run every 30 minutes by first adding a custom cron schedule named ‘every_30_minutes’ using the `add_custom_cron_schedule()` function and the `cron_schedules` filter. Then, it adds an action named `run_every_30_minutes_hook`. It checks if the event is already scheduled using `wp_next_scheduled()`. If not, it schedules the event using `wp_schedule_event()` with the ‘every_30_minutes’ recurrence.

Schedule a weekly user report generation

function add_weekly_cron_schedule($schedules) {
    $schedules['weekly'] = array(
        'interval' => 7 * DAY_IN_SECONDS,
        'display' => 'Once a week'
    );
    return $schedules;
}

add_filter('cron_schedules', 'add_weekly_cron_schedule');

function generate_user_report() {
    // Your user report generation logic here
}

add_action('generate_user_report_hook', 'generate_user_report');

if (!wp_next_scheduled('generate_user_report_hook')) {
    $next_week = strtotime('next Monday');
    wp_schedule_event($next_week, 'weekly', 'generate_user_report_hook');
} 

This code schedules a weekly user report generation by first adding a custom cron schedule named ‘weekly’ using the add_weekly_cron_schedule() function and the cron_schedules filter. Then, it adds an action named generate_user_report_hook. It checks if the event is already scheduled using wp_next_scheduled(). If not, it schedules the event using wp_schedule_event() with the ‘weekly’ recurrence, starting from the next Monday.

Leave a Comment

Your email address will not be published. Required fields are marked *