Follow @BPSPro

WordPress Cron Job – Change WordPress Cron Job Schedule, Change WordPress Cron Job Time

1 Comment RSS Site Feed Author: AITpro Admin
Published: December 6, 2011
Updated: December 8, 2011

I looked around for the best way to quickly change a WordPress Scheduled Cron job and amazingly did not come across an answer right away.  Maybe changing the WordPress Cron Schedule Time is such a simple thing to do that no one bothered creating a post about it.  There are  several different WordPress pre-made functions that could be used to either remove or unschedule a Cron job so I was just looking for the quickest, cleanest, best and / or recommended way to to this with the least amount of coding changes to the existing WordPress Scheduled Cron job functions and hooks.

Here is the WordPress Scheduled Cron scenario:

I had an existing WordPress Scheduled Cron job that was scheduled to run weekly to check for available plugin updates and wanted to change it to daily scheduled cron job.  Since existing users of the plugin already had scheduled crons jobs set to fire on a weekly basis I needed the old weekly WordPress scheduled cron job to be cleared or unscheduled or replaced with the new daily scheduled cron job.  Seems simple enough right, but when checking for available ways to do this I came across too many options and did not find “the best recommended” way to do this.  I cannot say with 100% certainty that this is the best way to change or reschedule a WordPress cron job, but it definitely has to be the simplest and easiest way to do this.

The old WordPress Weekly Scheduled Cron job code:

// WP weekly scheduled Cron job
function bpsPro_add_weekly_cron( $schedules ) {
	$schedules['weekly'] = array('interval' => 604800, 'display' => __('Once Weekly'));
	return $schedules;
}
add_filter('cron_schedules', 'bpsPro_add_weekly_cron');

// checking current installed version against currently available version
function bpsPro_update_checks() {
// your code that does your plugin version update checking
}
add_action('bpsPro_update_check', 'bpsPro_update_checks');

// Weekly Cron Job - bpsPro_update_check
function bpsPro_schedule_update_checks() {
	if (!wp_next_scheduled('bpsPro_update_check')) {
		wp_schedule_event(time(), 'weekly', 'bpsPro_update_check');
	}
}
add_action('init', 'bpsPro_schedule_update_checks');

The new WordPress Daily Scheduled Cron job code with the old code commented out and new code has been highlighted:

// Was a WP weekly scheduled Cron job is now a Daily scheduled cron job
function bpsPro_add_weekly_cron( $schedules ) {
	//$schedules['weekly'] = array('interval' => 604800, 'display' => __('Once Weekly'));
	$schedules['daily'] = array('interval' => 86400, 'display' => __('Once Daily'));
	return $schedules;
}
add_filter('cron_schedules', 'bpsPro_add_weekly_cron');

// checking current installed version against currently available version
function bpsPro_update_checks() {
// your code that does your plugin version update checking
}
add_action('bpsPro_update_check', 'bpsPro_update_checks');

// Was a Weekly Cron Job now a Daily Cron - bpsPro_update_check
function bpsPro_schedule_update_checks() {
	$bpsCronCheck = wp_get_schedule('bpsPro_update_check');

	if ($bpsCronCheck == 'weekly') {
	wp_clear_scheduled_hook('bpsPro_update_check');
	}
	if (!wp_next_scheduled('bpsPro_update_check')) {
		//wp_schedule_event(time(), 'weekly', 'bpsPro_update_check');
		wp_schedule_event(time(), 'daily', 'bpsPro_update_check');
	}
}
add_action('init', 'bpsPro_schedule_update_checks');

So if you are looking for the simplest and quickest way to change a WordPress Scheduled Cron job Time or frequency then this worked well for me without any problems down the road.  And of course this is something very simple, but like I said I was looking for the recommended best way to do this and did not come across that info.  If you know of a better recommended way to do this please leave a comment.  😉

Skip to toolbar