X

Schedule Cron Jobs with WordPress

Snippets by IsItWP

A cron job lets you run a bit of the code at a specific interval. For example, you could have a script that can remove the website trash with an hourly interval. To set up a cron job isn’t something you do on your lunch break. Luckily, you can do this in WordPress with the wp_schedule_event() function.

Note: You can schedule a hook that’ll be executed by the WordPress actions core on a specific interval specified by you. The action will trigger when someone visits your website, if the scheduled time has passed.

Here’s an example to set up a cron job on hourly interval.

Instructions: Add the following code in the functions.php file of your WordPress theme to see it in action. You can change the time as you need.

<?php
add_action('my_hourly_event', 'do_this_hourly');

function my_activation() {
	if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
		wp_schedule_event(time(), 'hourly', 'my_hourly_event');
	}
}
add_action('wp', 'my_activation');

function do_this_hourly() {
	// do something every hour
}
?>

Make sure to replace the function do_this_hourly() with the task you want to achieve with this cron job.

You may also enjoy flushing permalinks once per hour cron job.

[code][/code]

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!