WordPress External Cron Plugin

There are plenty of scripts out there that require you to request a page on a daily basis to perform certain tasks. Normally you can accomplish this by setting up a simple cron. And on a good host, that’s easy. But what if your host doesn’t have cron? Well if you run WordPress, you now can use my Hit an External Cron plugin. You can download it from Google Code. It’s a simple plugin that adds an options page under settings to enter the url you need to visit on a daily basis.

Not only am I releasing this plugin under the GPL, but if you continue reading I’ll explain to you everything under the hood.

This write up is based on revision number 7. It’s 77 lines in total. That includes white space, and comments.

I’m going to go through each chunk of code and explain what it does and where you can find more information about the feature of WordPress that I used.

<?php
/*
Plugin Name: Hit an External Cron
Plugin URI: http://aaron.jorb.in/2010/wordpress-external-cron-plugin/
Description: Use wordpress's internal cron system to hit an external cron on a daily basis
Author: Aaron Jorbin
Version: 0.1
Author URI: http://aaron.jorb.in/
License: GPL2
*/

Every plugin needs to have headers to populate the fields on the Plugin page and help users know a bit more about what is going on.The WordPress Codex has an example. All of the fields are self explanitory.

function install_jorbin_cron(){
	wp_schedule_event(time(), 'daily', 'jorbin_daily_event');
}
register_activation_hook(__FILE__, 'install_jorbin_cron');

function uninstall_jorbin_cron(){
	$timestamp = wp_next_scheduled( 'jorbin_daily_event');
	wp_unschedule_event($timestamp, 'jorbin_daily_event');
}
register_deactivation_hook(__FILE__, 'uninstall_jorbin_cron');

This next section tells the plugin what to do on activation and deactivation. The Codex explains these functions. The first paramater is the location of the function (in this case the file we are already using), and the second is the function we want to call.

Our install function schedules an event that we want to do every day, once a day. We are going to call this event jorbin_daily_event. Later on, we will attach an action to this event.

When the plugin is deactivated, we remove our event.

If you want to learn more about the psudo-cron system implemented in wordpress, check out Scheduling with WordPress Cron Functions .

add_action('admin_menu', 'jorbin_cron_settings_menu');

function jorbin_cron_settings_menu(){
	add_options_page('External Cron Settings', 'External Cron Settings', 'manage_options', 'cron', 'jorbin_cron_options_page');
}

function jorbin_cron_options_page(){
	echo "<div>";
	echo "<h2>Hit an External Cron Settings</h2>";
	echo '<form action="options.php" method="post">';
	settings_fields('jorbin_cron_options');
	do_settings_sections('cron');
	echo '<input name="Submit" type="submit" value="'. esc_attr('Save Changes') .'" />
</form></div>';
}


/* Fill the Menu page with content */

function jorbin_cron_init(){
	register_setting( 'jorbin_cron_options', 'jorbin_cron_options', 'jorbin_cron_options_validate' );
	add_settings_section('the_jorbin_cron', '', 'jorbin_cron_details_text', 'cron');
	add_settings_field('jorbin_cron_field', 'URL', 'jorbin_cron_field_display', 'cron', 'the_jorbin_cron');
}
add_action('admin_init', 'jorbin_cron_init');


function jorbin_cron_field_display(){
	$options = get_option('jorbin_cron_options');
	echo "<input id='jorbin_cron_field' name='jorbin_cron_options[url]' size='40' type='text' value='{$options['url']}' />";
}

function jorbin_cron_details_text(){
	echo "<p>Enter the URL you need to hit once a day</p>";
}
function jorbin_cron_options_validate($input){
	$newinput['url'] = esc_url_raw( trim( $input['url'] ) );
	return $newinput;
}

Next up is the addition of our options page and filling it with content. I started by looking over WordPress Settings API Tutorial from wordpress.org forum moderator Otto. Thus I was easily able to:

  • Add a page titled “External Cron Settings” under the settings section (jorbin_cron_settings_menu)
  • Add a basic overview of that page (jorbin_cron_options_page)
  • Add an option and the appropriate fields and validation (jorbin_cron_init, jorbin_cron_field_display, jorbin_cron_details_text, and jorbin_cron_options_validate)

Using these functions allows me to do what I do best (add features), and let’s wordpress do what it does best (everything else). After you’ve read over Otto’s tutorial, I suggest reading the Codex Documention on the Settings API.

/* The Guts of the plugin
 * Where the cron actually happens
*/
function jorbin_cron(){
	$options = get_option('jorbin_cron_options');
	$url = $options['url'];
	$request = new WP_Http;
	$result = $request->request( $url );
}
add_action('jorbin_daily_event', 'jorbin_cron');

Finally we actually perform our action. By using the WP_Http class, we don’t need to worry about individual server configurations and can focus on requesting the url that we want. If you remember earlier when we added the jorbin_daily_event event to happen on a daily basis, now we take advantage of it by having the jorbin_cron function take place every time jorbin_daily_event happens. If you want to learn more about WP_Http, Ozh has a great post about it.

That was it. The entire plugin. If you have any questions, notice any bugs, or have any feature requests, please post them below.

Published by

Aaron Jorbin

Polyhistoric man of the web. Currently Technical Architect on the Conde Nast Platform Team and a WordPress Core Committer, he works to improve developer happiness and is dedicated to making the internet usable and enjoyable by everyone. He's probably wearing a bow tie right now.

10 thoughts on “WordPress External Cron Plugin”

  1. Hi there,
    Thanks for this contribution.

    I am using feedwordpress, which needs a cron support (which my host doesn’t provide, since this is a cron for fetching posts from various links).

    Will your plugin help me?

    p.s: would you consider installing “subscribe to comments” here ?

    All the best,
    Tal

    1. I looks like feedwordpress also implements the WordPress psuedo cron that this plugin uses. This plugin is more so designed for cases where a pseudo cron isn’t already implemented.

      Also, I took your suggestion and subscribe to comments is now activated.

  2. I just uploadet you Hit an External Cron. It doesn’t seem to do anything. Is there something else to configure or should the job run by itself as soon as the Plugin is activated?
    “Enter the URL you need to hit once a day” What URL I have to fill ?
    Thanx :-)

    1. @ Kumpol – did you find a way to put in a row of URLs to be hit ? I hesitate to ask Aaron as this question might be an insult or at is least putting me into the amateur corner ?
      Tx
      WWW

      1. The easiest way to do multiple urls is to deconstruct this plugin and build one that does just that. Take the jorbin_cron function and instead of looking for options, look at an array of urls that you can then loop thought requesting each of them separately.

  3. Does this still work the newer versions of WordPress? I noticed that this post is about a year old, and WP 3 has come out since then.

    1. I haven’t heard from anyone that this doesn’t work and none of the changes in 3.0 or 3.1 should break these features. Also, WordPress 3.0 is just the next release after 2.9. 2.9 to 3.0 was no different then 3.0 to 3.1

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>