Scheduling WordPress Hooks With WP_Cron

If you do much in the way of WordPress plugin development, whether you’re doing plugins, themes, or just customizing your own sites, you may have come across a scenario where you wanted to have a function fire every so often. WordPress has a system, known as WP_Cron, that you can hook functions into, setting them to run at scheduled intervals. (This is great for sending out email newsletters containing your post content, parsing RSS feeds on a regular basis, etc.)

To schedule a function to fire every hour, you would run wp_schedule_event() with the appropriate arguments. Do this somewhere convenient, where it won’t be running all the time, such as when a plugin is first activated.

register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');

function my_activation() {
wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}

function do_this_hourly() {
// do something every hour
}

This isn’t true cron, which means your scheduled events won’t execute if you don’t have people loading pages on your site around the timeframe an event is scheduled for.