Get In Touch
113 Cherry St #92768,
Seattle, WA 98104-2205
[email protected]
Back

Fun with Cron Jobs:using WordPress’ Cron function …for mischief

Here is the situation: You want to piss off someone at work. The day before, he was sending you tasks just when you were packing up your things to go home. You couldn’t think of anything else to do but send 3,000 emails to him with one sentence “Choose a better time!”

Here’s another situation: You want to send the special ‘her’ a message in an email every hour.

You can either type your message and click send every time you want to send the emails or you can employ Cron Jobs.

I have been working on a pet project for quite a while. It utilizes Cron Jobs. Not having a dedicated linux server to practice on, I went to unearth some of WordPress’ scripts where they make use of Cron Jobs and I can tell you right here how to use WordPress’ built-in functions to create scheduled tasks.

  • So first you need to have a WordPress installation that is live on a web server.
  • Next, create functions.php (if it hasn’t been created) in your current theme’s directory.
  • Place the code below in the file.

 

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() {
	// this is what you do hourly
 mail($to, $subject, $message); //you define your $to, $subject and $message variables yourself. Or you could just put in your own code.
}

So basically we have three functions used here. do_this_hourly() is a self created function. This is the function you want running every hour. my_activation() is a self created hook which you will use to add an action hook.

So here we go. Go ye disciples and do likewise.

This post is powered by Cron. I typed this days ago!

This website stores yummy cookies on your computer. Cookie Policy