Use WordPress Cron

WordPress-Christmas-14WordPress has its own cron to automatically and scheduled run certain themes. Therefore WordPress provides several functions to use the cron.

In our first example we send every hour a mail with the help of the WordPress function wp_mail(). FYI, this is just a possibility, please don't do it on your system!

As default, WordPress can handle 3 time keys, which you can call with the function wp_schedule_event.

// send automatic scheduled email
if ( ! wp_next_scheduled('my_task_hook') ) {
	wp_schedule_event( time(), 'hourly', 'my_task_hook' ); // hourly, daily and twicedaily
}

add_action( 'my_task_hook', 'my_task_function' );
function my_task_function() {
	
	wp_mail( 
		'example@yoursite.com', 
		'Automatic mail', 
		'Hello, this is an automatically scheduled email from WordPress.'
	);
}

If you use the cron in a Plugin or theme, then don't forget to deactivate the cron if you don't need it anymore.

delete_action( 'my_task_hook', 'my_task_deactivate' );
// clean the scheduler
function my_task_deactivate() {
	
	wp_clear_scheduled_hook( 'my_task_hook' );
}

But not always are 3 time values enough. Luckily you can expand the control via a filter.

add_filter( 'cron_schedules', 'filter_cron_schedules' );
// add custom time to cron
function filter_cron_schedules( $schedules ) {
	
	$schedules['once_half_hour'] = array( 
		'interval' => 1800, // seconds
		'display'  => __( 'Once Half an Hour' ) 
	);
	
	return $schedules;
}

Comments are closed.

10 comments

  1. matt mcinvale

    exactly what i was looking for. need to automatically post and remove pages from a new project. wasn't exactly sure how wordpress handled cron. :)

  2. Guillaume

    Is it a true cron that will start on its own, or on every page load it checks if some job needs to be run?

    Another way to put it:
    When you set a cron to run every hour and no one visits your site for 3 days, will it run every hour during that time?

  3. matt mcinvale

    WP's cron functionality requires pages to be loaded. You can counteract this by setting a real cronjob on your system to ping the site in question every couple minutes.

    */5 * * * * curl http://somesite.com/

  4. Guillaume

    Answered my own question. From the codex:

    The action will trigger when someone visits your WordPress site, if the scheduled time has passed.

    This is an important nuance. In my case, I need to fetch data from an external site every hour. The first visitor after the scheduled time will see the old data. It will trigger the cron, and only then will the new data be available.

  5. shawn

    I can get it to send me sms and email every hour but no matter what I do, the function filter_cron_schedules doesn't seem to be firing. I don't want to use the 3 standard time keys. I'd like to customize the seconds as needed. What is the syntax in the wp_schedule_event to get the filter to be called? Thanks!

  6. David Cowgill

    I can't seem to get the function to trigger even though the hook seems to be registered.

    Another tip so you can see the next scheduled date/time is to print out your hook via the wp_next_scheduled function. This is great for troubleshooting. WP seems to use GMT and my offset is -7 so once I printed out the actual date, I saw that the scheduler wasn't going to fire for another 7 hours.
    echo date("d-M-Y h:i:s A", wp_next_scheduled(my_task_hook));

    Also I think you have a mistake in your code above:
    delete_action( 'my_task_hook', 'my_task_deactivate' );
    should be
    remove_action( 'my_task_hook', 'my_task_deactivate' );

4 pingbacks

  1. Использование Cron в WordPress Wordpress: статьи, плагины, темы
  2. add custom time to wp cron | Depozit
  3. How to Get Your Twitter Follower Number in Plain Text | Pro Blog Design
  4. How to Get Your Twitter Follower Number in Plain Text | WordPress Toolbag