Polyhistoric Man Of The Web

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.

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

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.

<br />
function install_jorbin_cron(){<br />
	wp_schedule_event(time(), &#8216;daily&#8217;, &#8216;jorbin_daily_event&#8217;);<br />
}<br />
register_activation_hook(__FILE__, &#8216;install_jorbin_cron&#8217;);</p>
<p>function uninstall_jorbin_cron(){<br />
	$timestamp = wp_next_scheduled( &#8216;jorbin_daily_event&#8217;);<br />
	wp_unschedule_event($timestamp, &#8216;jorbin_daily_event&#8217;);<br />
}<br />
register_deactivation_hook(__FILE__, &#8216;uninstall_jorbin_cron&#8217;);<br />

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 .

<br />
add_action(&#8216;admin_menu&#8217;, &#8216;jorbin_cron_settings_menu&#8217;);</p>
<p>function jorbin_cron_settings_menu(){<br />
	add_options_page(&#8216;External Cron Settings&#8217;, &#8216;External Cron Settings&#8217;, &#8216;manage_options&#8217;, &#8216;cron&#8217;, &#8216;jorbin_cron_options_page&#8217;);<br />
}</p>
<p>function jorbin_cron_options_page(){<br />
	echo &quot;&lt;div&gt;&quot;;<br />
	echo &quot;&lt;h2&gt;Hit an External Cron Settings&lt;/h2&gt;&quot;;<br />
	echo &#8216;&lt;form action=&quot;options.php&quot; method=&quot;post&quot;&gt;&#8217;;<br />
	settings_fields(&#8216;jorbin_cron_options&#8217;);<br />
	do_settings_sections(&#8216;cron&#8217;);<br />
	echo &#8216;&lt;input name=&quot;Submit&quot; type=&quot;submit&quot; value=&quot;&#8217;. esc_attr(&#8216;Save Changes&#8217;) .&#8217;&quot; /&gt;<br />
&lt;/form&gt;&lt;/div&gt;&#8217;;<br />
}</p>
<p>/* Fill the Menu page with content */</p>
<p>function jorbin_cron_init(){<br />
	register_setting( &#8216;jorbin_cron_options&#8217;, &#8216;jorbin_cron_options&#8217;, &#8216;jorbin_cron_options_validate&#8217; );<br />
	add_settings_section(&#8216;the_jorbin_cron&#8217;, &#8221;, &#8216;jorbin_cron_details_text&#8217;, &#8216;cron&#8217;);<br />
	add_settings_field(&#8216;jorbin_cron_field&#8217;, &#8216;URL&#8217;, &#8216;jorbin_cron_field_display&#8217;, &#8216;cron&#8217;, &#8216;the_jorbin_cron&#8217;);<br />
}<br />
add_action(&#8216;admin_init&#8217;, &#8216;jorbin_cron_init&#8217;);</p>
<p>function jorbin_cron_field_display(){<br />
	$options = get_option(&#8216;jorbin_cron_options&#8217;);<br />
	echo &quot;&lt;input id=&#8217;jorbin_cron_field&#8217; name=&#8217;jorbin_cron_options[url]&#8217; size=&#8217;40&#8217; type=&#8217;text&#8217; value='{$options[&#8216;url&#8217;]}&#8217; /&gt;&quot;;<br />
}</p>
<p>function jorbin_cron_details_text(){<br />
	echo &quot;&lt;p&gt;Enter the URL you need to hit once a day&lt;/p&gt;&quot;;<br />
}<br />
function jorbin_cron_options_validate($input){<br />
	$newinput[&#8216;url&#8217;] = esc_url_raw( trim( $input[&#8216;url&#8217;] ) );<br />
	return $newinput;<br />
}<br />

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.

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

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.


If you have a reaction, you should get a blog and send me a webmention or pingback.


*/