
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.

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.
21 Responses
Shot guys
This is extremely useful
Great recipe, thank you.
Could you please do a more “realistic” example of a scheduled task (something more than just simply sending an email)?
Thanks, I need something like this to schedule spam cleanup and was lazy to look for it.
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….
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!
These kinds of core hacks are a bit pointless with the amount of upgrades wordpress is currently forcing on us!?
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.
Gorgeous Trick.. we can even schedule whatever we do other than post… so cool..
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.
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?
@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.
thank`s so much
good code
@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!
Is it same like wp-db manager lezterchan?
Trackbacks: