Many web applications require background tasks to run automatically—such as sending weekly email digests, clearing old temporary logs, synchronizing external API data, or generating reports. In WordPress, this is handled by WP-Cron, the built-in task scheduling system.
While WordPress provides default recurrence intervals (like hourly, twicedaily, and daily), you can also define custom intervals and schedule your own recurring background events using wp_schedule_event().
In this tutorial, we will walk through creating a custom cron job step-by-step.
1. Adding a Custom Recurrence Interval (Optional)
By default, WordPress only supports hourly, twicedaily, and daily. If you need a custom interval (for example, every 30 minutes or every 3 days), you can add it using the cron_schedules filter.
Add the following code to your custom plugin or theme’s functions.php:
if (!defined('ABSPATH')) {
exit;
}
add_filter('cron_schedules', 'wg_add_custom_cron_interval');
function wg_add_custom_cron_interval($schedules) {
// Add a 30-minute interval
$schedules['wg_every_thirty_minutes'] = [
'interval' => 30 * MINUTE_IN_SECONDS,
'display' => __('Every 30 Minutes', 'webgarage'),
];
return $schedules;
}
2. Scheduling the Event on Plugin Activation
Cron events should be scheduled when your plugin or theme is activated, and cleared when it is deactivated. We hook into register_activation_hook to schedule our custom task.
add_action('wp', 'wg_schedule_custom_cron_job');
function wg_schedule_custom_cron_job() {
// Check if the event is already scheduled; if not, schedule it
if (!wp_next_scheduled('wg_run_background_cleanup_task')) {
wp_schedule_event(time(), 'wg_every_thirty_minutes', 'wg_run_background_cleanup_task');
}
}
// Clear the cron job upon plugin deactivation
register_deactivation_hook(__FILE__, 'wg_unschedule_custom_cron_job');
function wg_unschedule_custom_cron_job() {
$timestamp = wp_next_scheduled('wg_run_background_cleanup_task');
if ($timestamp) {
wp_unschedule_event($timestamp, 'wg_run_background_cleanup_task');
}
}
3. Writing the Callback Function for Your Task
Now, you need to hook into the action name you defined (wg_run_background_cleanup_task) and write the code that executes when the cron runs—such as cleaning up old database rows from a custom table.
add_action('wg_run_background_cleanup_task', 'wg_execute_background_cleanup');
function wg_execute_background_cleanup() {
global $wpdb;
$table_name = $wpdb->prefix . 'wg_submissions';
// Example task: Delete submissions older than 30 days
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $table_name WHERE time < %s",
date('Y-m-d H:i:s', strtotime('-30 days'))
)
);
}
Best Practices for WP-Cron
- Understand WP-Cron Triggers: WP-Cron is not a true system cron (unless configured on your server via crontab). It triggers on frontend page views. For high-traffic sites, consider replacing the default trigger with a real server cron job (DISABLE_WP_CRON).
- Always Unschedule: Always clean up your scheduled events on deactivation to prevent orphaned cron jobs from running in the background.
Using wp_schedule_event() and WP-Cron allows you to automate routine tasks, keep your database clean, and execute background operations reliably.
Be sure to check out our previous tutorials on WordPress development:
How to Use WordPress Transients API for Performance
Building Custom REST API Endpoints in WordPress
Creating Custom Database Tables in WordPress with dbDelta
Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.