2013
01.20

As I’ve explained earlier here I’m working on an RSS based project, and I’ve got a request to rethink the feed processing method, and try to speed things up.

The first thing that come up, was to run a feed checker process that runs every two minutes.

So, how the scheduling works in WordPress?

First, we can modify the available intervals with “cron_schedules” filter. Then, we have to check if our process trigger is scheduled, if not, we have to schedule it. Then, we add an action, that is catching this. Basically, that’s it.

Let’s code.

1
2
3
4
5
6
7
8
9
10
add_filter( 'cron_schedules', 'my_add_cron_schedule' );

function my_add_cron_schedule( $schedules )
{
    $schedules['two-minutely'] = array(
        'interval' => 120, // 120 seconds means 2 minutes
        'display'  => __( 'Two minutely' ),
    );
    return $schedules;
}

So this will create the custom interval.
Let’s schedule the action, to trigger our function.

1
2
3
4
5
6
7
if (!wp_next_scheduled('my_twominutely_action'))
{
    $nexttime = strtotime("+2 minute");
    $start_time = mktime(date('H',$nexttime),0,0,date('m',$nexttime),date('d',$nexttime),date('Y',$nexttime));

    wp_schedule_event($start_time, 'two-minutely', 'my_twominutely_action');
}

The only thing what’s left from the great picture, is the action, to catch this event, and our shiny new function.

1
2
3
4
5
6
add_action('my_twominutely_action','my_function_to_run');

function my_function_to_run()
{
   echo 'Hello world.';
}

Wow! Nothing left, we’ve created everything.

No Comment.

Add Your Comment