Using Cron to schedule events in your WordPress blog

August 5, 2009 at 9:08 am

You probably know that WordPress can schedule events. In this recipe, I’ll show you how you create an event that will be executed once hourly, or daily, etc.

Just paste this piece of code in your functions.php file:

if (!wp_next_scheduled('my_task_hook')) {
	wp_schedule_event( time(), 'hourly', 'my_task_hook' );

add_action( 'my_task_hook', 'my_task_function' ); 

function my_task_function() {
	wp_mail('', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');

On line 1, we created an event, after verifying that an event of the same name wasn’t already registered. All we have to do then is to create a function to do what you want (In this example, the function is called my_task_function() and it simply send a dummy email) and hook this function to any WordPress event.