<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Page Blocks WordPress Plugin &#187; Lessons Learned</title>
	<atom:link href="http://wordpress.softwud.com/category/lessons-learned/feed" rel="self" type="application/rss+xml" />
	<link>http://wordpress.softwud.com</link>
	<description>By SoftWUD</description>
	<lastBuildDate>Tue, 05 Jan 2010 08:42:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Problems With Scheduling Events</title>
		<link>http://wordpress.softwud.com/problems-with-scheduling-events</link>
		<comments>http://wordpress.softwud.com/problems-with-scheduling-events#comments</comments>
		<pubDate>Thu, 15 May 2008 20:56:49 +0000</pubDate>
		<dc:creator>Paolo</dc:creator>
				<category><![CDATA[Lessons Learned]]></category>

		<guid isPermaLink="false">http://wordpress.softwud.com/?p=7</guid>
		<description><![CDATA[Recently needing to have a plugin re-try executing some code I thought that the "pseudo cron" scheduling feature of WordPress would be ideal. However it seems that some hosting companies - mine included - implement security policies that prevent it from working. Find out what is going wrong and a possible workaround.]]></description>
			<content:encoded><![CDATA[<p>I recently needed to have a plugin re-try executing some code until the code executed successfully, and for this I thought that the &#8220;pseudo cron&#8221; scheduling feature of WordPress would be ideal.</p>
<p>I decided to try this out on one of my hosting servers, and I&#8217;m glad I did. To my puzzlement the scheduling code just wouldn&#8217;t work&#8230; what the heck was going wrong?!?!</p>
<h4>Finding the cause</h4>
<p>After instrumenting the WordPress source code I finally narrowed down the cause&#8230; the paranoid security configuration of my hosting company.</p>
<p>This is a problem?!?! Well the trouble is that the &#8220;cron&#8221; code depends on PHP&#8217;s fsockopen() network function. The WordPress code uses this function to run the code in the wp-cron.php file, and it is this code which looks at the events that have been scheduled and tries to carry them out.</p>
<p>Since fsockopen() is blocked from working, even from making connections looping back to the same server, so to is the whole &#8220;pseudo cron&#8221; event scheduling feature of WordPress. So if you need to have code that runs at regular intervals e.g. database backup, creating RSS feeds, sending out newsletters etc. you may face a rude shock.</p>
<h4>A solution ?</h4>
<p>In the ideal world I&#8217;d expect that WordPress would provide a fallback mechanism for servers that have this problem, however in the real world I&#8217;m not so optimistic. I haven&#8217;t checked how prevalent this problem may be so for now I&#8217;m documenting this issue in case others are scowering the internet for answers.</p>
<p>For coders who must absolutely rely on this &#8220;psuedo cron&#8221; there may be a workaround. I&#8217;ve found that in the WordPress code there is a short-circuit that will call a callback function instead of attempting to execute the wp-cron.php code. To do this you need to do the following (see the code sample below):</p>
<ul>
<li>Create a callback function to schedule that returns false</li>
<li>Register a custom &#8220;schedule interval&#8221; by overriding the &#8216;cron_schedules&#8217; filter</li>
<li>Schedule a single event</li>
</ul>
<p><code>function my_callback()<br />
{<br />
  // Do something here...<br />
  //<br />
  // VERY IMPORTANT:<br />
  //<br />
  // If this is a single event you will need to "unschedule" the event<br />
  // at this point. To do this you will need to store all of the values<br />
  // that you need to pass to wp_unschedule_event(...) such as<br />
  // the time the event is scheduled for, the hook name (in this<br />
  // example 'my_schedule_hook_name'), and any optional<br />
  // parameters.<br />
  //<br />
  // Also make sure that this function returns false otherwise the<br />
  // short-circuit won't work.<br />
  //<br />
  return false;<br />
}<br />
</code><br />
<code>function my_schedules($wp_schedules)<br />
{<br />
  $new_schedules = $wp_schedules;<br />
  $new_schedules['my_schedule_hook_name'] =<br />
    array('interval' =&gt; 86400,  'display' =&gt; __('My Interval Name'),  'callback' =&gt; 'my_callback');<br />
  return $new_schedules;<br />
}<br />
//<br />
add_filter('cron_schedules', 'my_schedules');<br />
</code><br />
<code>// Trigger the scheduling on a daily basis - Note for plugins do this<br />
// somewhere where it won't execute each time the plugin is loaded<br />
// since plugins are loaded each time a page is refreshed.<br />
wp_schedule_single_event(time() + 86400, 'my_schedule_hook_name');<br />
</code><br />
Hopefully this workaround that I&#8217;ve found will help you build more robust code. Please let me know how you go.</p>
]]></content:encoded>
			<wfw:commentRss>http://wordpress.softwud.com/problems-with-scheduling-events/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
