
What is WP_Cron in WordPress and how to use it
When working with WordPress, one of the most important — yet often invisible — features is the task scheduling system known as WP_Cron
. In this article, I’ll explain what it is, how it works, and how you can use it to automate tasks on your site. I’ll also show you how to disable the default WP_Cron
and use your server’s real cron system for better reliability.
What is WP_Cron?
WP_Cron
is WordPress’s internal system for handling scheduled tasks. These tasks include things like publishing scheduled posts, sending emails, deleting old data, and much more.
But here’s the catch: it’s not a real cron job. Unlike Linux’s cron system, which runs tasks at fixed time intervals, WP_Cron
only runs when someone visits your site. That means if no one visits your site, scheduled tasks won’t run on time.
How does WP_Cron work?
Every time someone loads a page on your site, WordPress checks if any scheduled tasks are due. If there are, it runs them asynchronously in the background.
This behavior is handled by the wp_cron()
function, which runs automatically during the page load — as long as no other cron process is already active.
How to schedule a custom WP_Cron task
You can use wp_schedule_event()
to register your own tasks. Here’s a practical example:
1. Register the event when your plugin or theme is activated:
<?php function activateMyPlugin() { if ( ! wp_next_scheduled( 'myCustomCronHook' ) ) { wp_schedule_event( time(), 'hourly', 'myCustomCronHook' ); } } register_activation_hook( __FILE__, 'activateMyPlugin' );
2. Create the function that will be executed:
<?php function executeMyScheduledTask() { // Custom logic, like sending reports or cleaning up data error_log( 'Running scheduled task...' ); } add_action( 'myCustomCronHook', 'executeMyScheduledTask' );
3. Cancel the event when your plugin is deactivated:
<?php function deactivateMyPlugin() { $timestamp = wp_next_scheduled( 'myCustomCronHook' ); if ( $timestamp ) { wp_unschedule_event( $timestamp, 'myCustomCronHook' ); } } register_deactivation_hook( __FILE__, 'deactivateMyPlugin' );
Available Scheduling Intervals
WordPress provides a few built-in intervals:
hourly
: every hourtwicedaily
: twice a daydaily
: once a day
If you need a custom interval, you can register one like this:
<?php function addCustomCronInterval( $schedules ) { $schedules['everyFiveMinutes'] = [ 'interval' => 300, // seconds 'display' => 'Every 5 minutes', ]; return $schedules; } add_filter( 'cron_schedules', 'addCustomCronInterval' );
Now you can use 'everyFiveMinutes'
in wp_schedule_event()
.
How to view and debug scheduled tasks
You can use the WP Crontrol plugin, which allows you to:
- View all scheduled tasks
- Edit, delete, or run tasks manually
- Add new custom tasks from the admin panel
Disabling WP_Cron and using the Server’s real Cron System
If your site has a lot of traffic or you need more precise control, it’s best to disable WP_Cron and use the server’s native cron system instead.
Step 1: Disable WP_Cron in wp-config.php
Add this line before the /* That's all, stop editing! */
comment:
<?php define( 'DISABLE_WP_CRON', true );
Step 2: Set up a cron job on your server
Using cPanel, Plesk, or SSH access, you can create a cron job to run every 5 minutes. Here’s an example command:
*/5 * * * * curl -s https://yoursite.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
This will call wp-cron.php
regularly without relying on site traffic.
WP_Cron
is a powerful tool for automating tasks within WordPress — if you understand how it works. It’s fine for small websites with low traffic, but for better performance and reliability, the server’s cron system is the way to go.