Scheduling Tasks in WordPress: a Plugin Developer's Guide

Notice: This is a discussion thread for comments about the SitePoint article, Scheduling Tasks in WordPress: a Plugin Developer’s Guide.

What experience have you had using WordPress’s scheduling API? Any tips you’d add?

I recently added an “auto delete old log files” feature to one of my plugins. It took a little while to get my head around the API, but it wasn’t that bad for me.

In my “activate” I added

wp_schedule_event(time(), 'daily', 'er_cron_del_hook')

In my “deactivate”

remove_action('er_cron_del_hook', 'mitt_er_cron_delete_logs');

add an action

add_action('er_cron_del_hook', 'mitt_er_cron_delete_logs');

and write the function

function mitt_er_cron_delete_logs()
	$cron_del_limiter = get_option('er_do_cron_del');

	if ( ($cron_del_limiter == 'month') || ($cron_del_limiter == 'week') )
		$curr_time = time();
		$cron_file_age = 31536000; // default 1 year should be way more than enough

		if ($cron_del_limiter == 'month') $cron_file_age = 2678400;
		if ($cron_del_limiter == 'week') $cron_file_age = 604800;
// do file delete stuff if conditional tests pass

Great tutorial - Thankyou.

@Mittineague: Cool hack. Will save my server resources a lot!

You could also hack php and use a GET post via Cpanel CRON