Skip to content
wp-skills
Register
tools/wordpress-cron-job-generator

WordPress Cron Job Generator

Generate WordPress cron job code. Cron job is a scheduled task that runs on a specific interval. You can use this tool to generate cron job code for your WordPress plugin.

Runs on init and schedules the event if it is not already scheduled. Give it a prefix of your own.

Your theme or plugin's own domain, for the custom schedule's label.

One key, used by the cron_schedules filter and by wp_schedule_event() alike. They have to agree, or the event is scheduled against a recurrence that was never registered and simply never runs.

Shown by tools that list schedules. Falls back to the name above, because __( '' ) does not return an empty string — gettext reads the catalogue's own header for an empty msgid.

WP-Cron only runs on a page load, so an interval shorter than the site's traffic gap is a ceiling rather than a promise.

Starts at the next midnight on the site's clock rather than right now. The timestamp is still UTC — it has to be, wp_schedule_event() documents it that way — this just picks a different instant.

The callback that does the work. It is attached with add_action, so it runs whenever the hook fires — including if you fire it yourself.

The event's own name. Prefix it: scheduling against a hook another plugin also uses runs their code on your schedule.

Comma separated. These identify the event as well as feed the callback — wp_next_scheduled() and wp_unschedule_event() both match on the hook and its arguments together.

snippet.php
function wp_skills_my_custom_cron_job() {
	// Your code here.
}
add_action( 'my_custom_cron_job', 'wp_skills_my_custom_cron_job' );

/**
 * Schedules the cron event if it is not scheduled already.
 *
 * @return void
 */
function wp_skills_register_my_custom_cron_job(): void {
	if ( ! wp_next_scheduled( 'my_custom_cron_job' ) ) {
		wp_schedule_event( time(), 'daily', 'my_custom_cron_job' );
	}
}
// 'init' rather than 'wp': 'wp' only fires on front-end page loads, so an event
// scheduled there is never created by an admin-only or WP-CLI request.
add_action( 'init', 'wp_skills_register_my_custom_cron_job' );