DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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.


  1. Hello and thank you so much for this valuable CRON script. Could you please tell me how one would change the time value in the event
    “wp_schedule_event( time(), ‘hourly’, ‘my_task_hook’ );” to run a job every two weeks on a Tuesday at 6:00 PM ?

    Again, thank you for sharing such a useful script.

  2. It doesn’t work like this. For the pseudo-cronjob of WP you only have the options ‘daily’, ‘twicedaily’ or ‘hourly’. So make it daily and it will run at a tuesday as well. There is a way (as far as I remember) to start it at a certain time of the day.

    So, if you write a function to begin the hooking at 6pm, you will have it at Tuesday 6pm as well.

  3. You can create custom intervals using the cron_schedules filter. See the entry for wp_get_schedules() on the Codex for details.

    You can also set the time it will start with the timestamp you pass into wp_schedule_event().

  4. really great informations! Thanks very much from Italy 🙂

  5. Hi

    I am testing this function . What wuld be the function in cas we want to hit a certain url in order to refresh image. What do you put instead of

    wp_mail(‘you@yoursite.com’, ‘Automatic email’, ‘Hello, this is an automatically scheduled email from WordPress.’

Leave a Comment

Your email address will not be published. Required fields are marked *