Using Cron to schedule events in WordPress

August 12, 2009 · Print This Article

WordPress can schedule events. but how you create an event that will be executed once hourly, or daily, etc?

Just copy following code to your functions.php file:

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

add_action( 'live_task_hook', 'live_task_function' );

function live_task_function() {
wp_mail('you@yoursite.com', 'Automatic email', 'Hello, this is an automatically scheduled email from Live Experience.');
}

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 live_task_function() and it simply send a dummy email) and hook this function to any WordPress event.


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.