Post Pic

Using Cron to schedule events in your WordPress blog

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('you@yoursite.com', '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.

Related Posts

Related Posts

No related posts.

21 Responses

Aug 05 2009 09:13

Shot guys

This is extremely useful

Aug 05 2009 09:21

Great recipe, thank you.
Could you please do a more “realistic” example of a scheduled task (something more than just simply sending an email)?

Aug 05 2009 10:06

Thanks, I need something like this to schedule spam cleanup and was lazy to look for it. :)

Aug 05 2009 10:30

I’m going to go out on a limb here, and say that this looks like another thing that doesn’t work on a blog hosted by word press itself….

Aug 05 2009 12:04

Thanks for the info … great stuff.

Is there a way to have chron jobs be posted at random intervals?

I have a number of articles that I am posting over time. I would like to have posts that don’t always come at a certain time – it looks too automated.

Thanks in advance!

Aug 05 2009 13:22

These kinds of core hacks are a bit pointless with the amount of upgrades wordpress is currently forcing on us!?

Aug 05 2009 13:34

This is just pseudo-cron though – it still requires a visitor to visit the blog to fire off the hooks that looks for scheduled tasks. It is by no means a replacement for *nix’s cron.

Aug 06 2009 09:59

Gorgeous Trick.. we can even schedule whatever we do other than post… so cool..

Aug 06 2009 22:05

Could you use this to generate a to-do list to post in your sidebar, say, of garden tasks that have to be done? New to WP and php, so really don’t know that much about it.

Aug 07 2009 07:51

I’m currently testing a plugin that I wrote that uses wp_cron to archive tweets for a particular search term.

One feature I would like to add to the plugin is the ability to schedule more frequently than hourly. I would use good old fashioned real Cron to do this, but haven’t quite worked out how to integrate this nicely.

I was hoping that I could use real Cron to execute cron.php every 5 minutes say and then register the hook with something like:

wp_schedule_event( time(), ‘3600′, ‘my_task_hook’ );

Any thoughts?

Aug 07 2009 21:24

@Sebastian – you are correct. I would not rely on this “cron” schedule to run things on schedule. it all depends on web traffic to check this cron to see if it has ran. *nix’s cron is the real way to go.

Aug 13 2009 10:27

thank`s so much
good code

Oct 26 2009 20:02

@Sebastian – this isn’t a core hack, as it is placed in your theme’s functions.php file. Upgrades shouldn’t affect this code at all.

Good tip though. I’ve been looking for a quick solution to a scheduled task a client wanted. This will work just fine!

Oct 27 2009 14:37

Is it same like wp-db manager lezterchan? :)

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required