Made of Everything You're Not

If you're a stalker I'd prefer if you didn't kill me. Thanks.
  • Home
  • Projects
  • Portfolio
  • Resume
« Introducing WP-hResume
Google Didn’t Fuck You; You Did »

Half Assed Cron With WP Cron

I was working on a project recently that was using WordPress as the base platform and had a need for a scheduled task function. Ordinarily, this would be the type of functionality that I would just setup a cron job for; but since WordPress has a pseudo cron system in place I decided to investigate that as an option. Long story short; fuck that man.

Working With WP Cron

Working With WP Cron

As mentioned, WordPress has a pseudo cron mechanism available to WordPress developers for scheduling tasks called WP Cron. WP Cron works using series of functions available for plugin developers to create their own cron style tasks. In theory WordPress makes this easy(ish) using the built in cron API but the reality is that it’s not only confusing and awkward to implement but once you’re complete you have to allow for a bit of latitude on the scheduling.

Take a look at scheduling a task in WordPress. Basically, it works by adding a function to your activation hook, within a plugin, that like the below:

<?php 
register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');
 
function my_activation() {
	wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}
 
function do_this_hourly() {
	// do something every hour
}
?>

Pretty simple implementation really though not very “cron” like in syntax. The important part is the call to “wp_schedule_event()” which takes 4 parameters (start date, recurrence, hook and arguments). According to the API manual the function:

Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed. See the Plugin API for a list of hooks.

The problem I had was that this function only allows for 3 options on recurrence; hourly, twicedaily and daily. I needed my function to run at a specific time. (It was EXTREMELY important that it ran during a specific interval.)

This isn’t to say that it’s not possible to schedule a task for a specific time; it’s just a pain in the ass. You have to write all the logic to determine when to add a new task and removing expired tasks. Ughh. To me the question becomes “Should I write a couple hundred lines of code or just add a single line to my crontab?”.

Crontab baby.

Bookmark and Share

Related Posts

Mailpress 5.0 Email Validation Bug
Wp-Click-Track 0.7.1
The Fear of WordPress Plugins
WP-Click-Track 0.4 Released Today!
Who’s Using My Code?

Tags: cron, wordpress

This entry was written by Eric Lamb and posted on Monday, February 8th, 2010 at 1:06 am and is filed under Code, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments

  1. Harper Shelby says:
    August 10, 2010 at 8:49 am

    While I won’t argue that wp-cron is highly advanced technology, it is possible to add your own cron intervals to the very limited selection. I’ve used the following snippet:

    add_filter(‘cron_schedules’, array($this, ‘add_five_minute_cron’)); //I’m doing this inside a class

    //Add a 5 minute interval to the wp_cron options
    function add_five_minute_cron(){
    return array(‘five_minute’ => array(‘interval’=>300, ‘display’=>’Every 5 minutes’));
    }

    Reply
  2. Sem says:
    August 19, 2010 at 12:43 pm

    Harper,

    I am by no means a PHP champ but i would like to implement what you have proposed as i have a major increase in my processor/memory usage because of the wp_cron jobs. Where do i need to place the code sample you posted, in wp_cron.php? Thanks

    Reply

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008

Copyright © 2008 - 2012 Eric Lamb - All rights reserved