WordPress, by its very design, is a platform that offers a high degree of flexibility and customisation. One area that developers often need to customise is the scheduling of tasks within WordPress. The built-in cron system, WP-Cron, simulates a system cron by scheduling tasks at specific intervals. However, the default intervals may not always fit your needs. In this article, we’ll explore how to add custom cron schedules in WordPress, including additional times, such as every 15 minutes.
Understanding WP-Cron
Before we dive into custom schedules, it’s essential to understand how WP-Cron works. Unlike a system cron that runs tasks at precise times, WP-Cron executes tasks when a page is loaded. If no one visits your site, the scheduled tasks will not run until someone does. This might not be an issue for high-traffic sites, but for lower-traffic sites, this could lead to delays in task execution.
Adding Custom Cron Schedules
To create a custom cron schedule in WordPress, you need to hook into the cron_schedules
filter. This filter allows you to add new schedules or modify existing ones. Let’s walk through adding a few custom schedules:
Step 1: Hook into cron_schedules
Add the following code to your theme’s functions.php
file or a site-specific plugin:
function my_custom_cron_schedules($schedules) { // Add a 'every_fifteen_minutes' schedule $schedules['every_fifteen_minutes'] = array( 'interval' => 15 * 60, // 15 minutes in seconds 'display' => __('Every Fifteen Minutes', 'textdomain'), ); // You can add more schedules here if needed return $schedules; } add_filter('cron_schedules', 'my_custom_cron_schedules');
Step 2: Schedule Your Events
Now that you have a custom schedule, you can use it when scheduling an event with wp_schedule_event()
:
if (!wp_next_scheduled('my_custom_fifteen_minute_event')) { wp_schedule_event(time(), 'every_fifteen_minutes', 'my_custom_fifteen_minute_event'); } add_action('my_custom_fifteen_minute_event', 'my_custom_event_function'); function my_custom_event_function() { // Your code here }
Additional Time Additions
If you require more custom intervals, you can easily add them by extending the my_custom_cron_schedules
function. Here are a few examples:
- Every 10 minutes:
$schedules['every_ten_minutes'] = array( 'interval' => 10 * 60, 'display' => __('Every Ten Minutes', 'textdomain'), );
- Twice Daily:
$schedules['twice_daily'] = array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __('Twice Daily', 'textdomain'), );
- Weekly:
$schedules['weekly'] = array( 'interval' => WEEK_IN_SECONDS, 'display' => __('Once Weekly', 'textdomain'), );
Remember to replace 'textdomain'
with your theme or plugin’s text domain for proper internationalisation.
Conclusion
Custom cron schedules in WordPress allow for precise control over when your scheduled tasks run. By adding your own intervals, you can optimise your website’s performance and ensure tasks are executed as needed without being restricted to the default schedules. Remember that WP-Cron relies on site traffic to trigger tasks, so high-frequency schedules on low-traffic sites might not be as reliable as expected. For critical tasks, consider using a real system cron job if possible.
With custom cron schedules, your WordPress site can be more efficient and responsive to the timing needs of your specific tasks, whether it’s clearing cache, running backups, or publishing scheduled posts.