WordPress 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
10 responses to “Use WordPress Cron”
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. 🙂
[…] мотивам: wpengineer.com Подписатся на обновления блога:Подписаться по […]
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?
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/
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.
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!
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' );
[…] https://wpengineer.com/use-wordpress-cron/ The filter accepts an array of arrays. The outer array has a key that is the name of the schedule or for example ‘weekly’. The value is an array with two keys, one is ‘interval’ and the other is ‘display’. […]
[…] If you’d like to learn more about scheduling events with WordPress, take a look at WP Engineer’s tutorial. […]
[…] If you’d like to learn more about scheduling events with WordPress, take a look at WP Engineer’s tutorial. […]