
What is Cron?
Cron is a Unix-based time-based job scheduler that runs tasks at predefined times or intervals. In a traditional server environment, a cron job is a scheduled command or script run at a specific time.
What is WP-Cron?
WordPress uses a pseudo-cron system called WP-Cron. Instead of relying on the server’s cron daemon, WordPress runs scheduled tasks during page loads. This is handled by wp-cron.php.
WP-Cron ≠ System Cron — it runs only when someone visits the site.
How to Use it in WordPress?
WordPress provides several functions and hooks to manage scheduled tasks:
- Schedule a Custom Cron Job
// Schedule an event if it’s not already scheduled
if ( ! wp_next_scheduled( ‘my_custom_cron_hook’ ) ) {
wp_schedule_event( time(), ‘hourly’, ‘my_custom_cron_hook’ );
}
// Hook your function to the event
add_action( ‘my_custom_cron_hook’, ‘my_custom_cron_function’ );
function my_custom_cron_function() {
// Your task logic here
error_log(‘My cron job executed at ‘ . current_time(‘mysql’));
}
- Custom Time Intervals
WordPress has default intervals like hourly, twice daily, and daily. To add custom ones:
add_filter( ‘cron_schedules’, ‘custom_cron_intervals’ );
function custom_cron_intervals( $schedules ) {
$schedules[‘every_five_minutes’] = array(
‘interval’ => 300,
‘display’ => esc_html__( ‘Every 5 Minutes’ ),
);
return $schedules;
}
Now you can use ‘every_five_minutes’ in wp_schedule_event().
Advantages of WordPress Cron
- Easy Integration: No need to access the server or cPanel.
- Plugin/Theme Friendly: Can be bundled with themes and plugins.
- Hook-based System: Uses WordPress actions and filters.
- Multisite Support: Works across all subsites.
- Decoupled from OS: Runs even on shared hosting where system cron isn’t available.
Disadvantages of WordPress Cron
- Depends on Traffic: If no one visits the site, cron doesn’t run.
- Overhead: Runs on every page load unless disabled.
- Timing Not Precise: A scheduled job can run late depending on site visits.
- Resource Usage: Heavy tasks can delay page loads.
- Hard to Debug: Errors in cron tasks are often silent.
How to Overcome the Visitor-Triggered Cron Issue
The most significant limitation of WP-Cron is its dependency on visitors.
- Disable WP-Cron:
Edit wp-config.php:
define(‘DISABLE_WP_CRON’, true);
Notes: Disabling WP-Cron using define(‘DISABLE_WP_CRON’, true); can improve performance on high-traffic sites but comes with notable disadvantages. Once disabled, scheduled tasks like post publishing, backups, or plugin jobs no longer run automatically unless a real server-side cron is configured. This setup requires technical knowledge and access to the server’s control panel or SSH. If not properly configured, tasks may be missed or delayed, leading to functionality issues or performance bottlenecks. Additionally, some plugins or themes relying on WP-Cron may stop working as expected, making debugging more complex. Therefore, disabling WP-Cron without a proper replacement can cause significant disruptions.
This stops WP from running wp-cron.php on every page load.
- Set Up Real System Cron:
Run WP-Cron via a server-side cron job:
On Linux (using crontab):
*/5 * * * * wget -q -O – https://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
On cPanel:
- Go to Cron Jobs
- Add a new job with:
wget -q -O – https://yourdomain.com/wp-cron.php?doing_wp_cron
Set it to every 5 or 10 minutes, depending on need.
Practical Examples and Code Implementation
// 1. Add the cron job schedule (optional if using ‘hourly’)
add_filter(‘cron_schedules’, ‘custom_cron_intervals’);
function custom_cron_intervals($schedules) {
$schedules[‘every_five_minutes’] = array(
‘interval’ => 10,
‘display’ => __(‘Every 5 Minutes’)
);
return $schedules;
}
// 2. Hook your function to the cron
add_action(‘my_custom_cron_event’, ‘my_custom_cron_function’);
function my_custom_cron_function() {
if (defined(‘WP_DEBUG’) && WP_DEBUG) {
error_log(‘🔔 My Custom Cron run at: ‘ . current_time(‘mysql’));
}
}
// 3. Schedule the event
add_action(‘wp’, ‘schedule_my_custom_cron’);
function schedule_my_custom_cron() {
if (!wp_next_scheduled(‘my_custom_cron_event’)) {
wp_schedule_event(time(), ‘every_five_minutes’, ‘my_custom_cron_event’);
}
}
// 4. Clean up on plugin deactivate (optional)
register_deactivation_hook(__FILE__, ‘remove_my_custom_cron’);
function remove_my_custom_cron() {
$timestamp = wp_next_scheduled(‘my_custom_cron_event’);
if ($timestamp) {
wp_unschedule_event($timestamp, ‘my_custom_cron_event’);
}
}
Conclusion
WordPress Cron is a powerful tool when used properly. For small to medium sites, it provides a simple way to schedule tasks. But for time-critical or high-traffic applications, consider switching to real cron jobs.
Final Recommendations:
- Use WP-Cron for non-critical jobs
- For time-sensitive tasks, disable WP-Cron and use server cron
- Always monitor and log your cron tasks for errors
Consult with Our WordPress Experts On:
- WooCommerce Store
- Plugin Development
- Support & maintenance