Running a cron/schedule script in WordPress

Posted Monday 19th Nov 2012 by Dan

Today I wanted to talk about a function within WordPress called wp_schedule_event(). This is a really handy function, which can be used in your theme’s functions.php or even a plugin. There are a few functions in this ‘family’, the other useful one to take note of is wp_schedule_single_event().

I’ll just be talking about the main function today, but will skim over the top of the secondary function.

The primary function allows you to run an event at the time of your choice (like a cron).

The secondary function, comes in handy when you only want an event to fire 1 time only.

What is wp_schedule_event?

So you might be thinking, what is this function and what does it do?! Well it’s actually really handy, I’ve used it recently when developing a WordPress plugin. Basically, this function allows you to run your own code (function) off of the WordPress cron. This is so you don’t have to create your own cron which you need to set up on your server etc. This in-built cron function, will run at the time you specify. The action will fire once someone visits your website (front-end or back-end) if the time has passed. It’s worth pointing out that the action of someone visiting your site will fire off the event specified by you.

Usage for this function is very simple:


<?php wp_schedule_event($timestamp, $recurrence, $hook, $args); ?>

That’s all you need. Obviously you need to put in your own variables as parameters.

  • Timestamp – this is when you want the event to run
  • Recurrence – how often the event should occur
  • Hook – the name of the hook to execute
  • Args – any further variables to pass through

How to use it ‘in the wild’

I thought it would be useful to quickly go through the plugin I created so you can see how to use this function in context.

A quick explanation of what my plugin did was this:

  • Find all posts from a Custom Post type I’d created
  • Find a custom field of ‘end date’ from each post
  • Convert this using strtotime()
  • Compare the time of NOW with the ‘end date’
  • If the ‘end date’ was greater than NOW, add the post ID to an array
  • At my scheduled time, run a wp_update_post() to set all ID’s in my array to ‘draft’

I was basically setting all out of date posts to draft, so they wouldn’t show on the website.

There’s a few things you need to set up, the code below is the beginning of your custom plugin/function.


add_action('creare_daily_event', 'get_expired_posts'); // add a custom action to fire

function creare_post_update() { // our scheduler function
if ( !wp_next_scheduled( 'creare_daily_event' ) ) {
wp_schedule_event( time(), 'hourly', 'creare_daily_event'); // set the hook to fire and time
 }
}
add_action('wp', 'creare_post_update'); // run when WordPress is accessed

function get_expired_posts(){ // function to run

// your function code goes here

}

That’s all you need!

I hope this has been of some use. If you have any questions or comments, please leave below.

By Dan

Dan is a huge Wordpress fan. He enjoys creating hectic PHP functions and playing football amongst other things. See all of posts.

  • http://www.eukhost.com/ eUKhost

    That is a really good idea.

  • daniel_c_long

    Thanks! Glad you like it