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]
<?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
*/
[/php]

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.

[php]
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’);
[/php]

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 .

[php]
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;
}
[/php]

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.

[php]
/* 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’);
[/php]

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.