WordPress Code Snippets

A fresh mix of code snippets and tutorials

WordPress Cron Tips


Here are some quick tips for working with WordPress Cron. The trick to using the scheduling functions is to get the next scheduled cron using wp_next_scheduled().

Getting the next scheduled cron

To get the timestamp of the next scheduled cron, include this in your function:

$timestamp = wp_next_scheduled('wpmix_cron');

Once this variable is set, scheduling functions can work their magic..

Unschedule cron event

To unschedule a cron event in WordPress:

Reschedule cron event

Likewise, to reschedule a cron event in WordPress:

Schedule cron event

And here’s how to schedule a cron event (note: $timestamp not required):

Clear scheduled hook

To clear a scheduled hook:


Putting it all together

Here’s an example that brings everything together. For my plugin, Simple Ajax Chat, here’s how to auto-clear all chat messages every hour. With the plugin installed, add the following code to your theme’s functions.php file:

No editing is required, unless you want to change the clearing frequency by changing hourly to either “hourly”, “daily”, or “twicedaily”. You can see the script in action (if you wait awhile) in the Chat Demo.

Update: Here is another example of using WordPress Cron.

Note: WP Crontrol is an excellent plugin for working with WP Cron. Highly recommended :)

Related Stuff

Learn more

WordPress Themes In Depth

Show Support

Like our new Facebook Page to show support!