<?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>Turing tarpit &#187; PHP</title>
	<atom:link href="http://www.turingtarpit.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.turingtarpit.com</link>
	<description>Where everything is possible but nothing of interest is easy –Alan Perlis</description>
	<lastBuildDate>Thu, 11 Mar 2010 02:01:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Wordpress Logger: A plugin that displays log messages to the Safari and Firefox console from PHP</title>
		<link>http://www.turingtarpit.com/2009/05/wordpress-logger-a-plugin-to-display-php-log-messages-in-safari-and-firefox/</link>
		<comments>http://www.turingtarpit.com/2009/05/wordpress-logger-a-plugin-to-display-php-log-messages-in-safari-and-firefox/#comments</comments>
		<pubDate>Tue, 12 May 2009 06:04:05 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.turingtarpit.com/?p=369</guid>
		<description><![CDATA[This is an essential plugin for developers who are frustrated with debugging plugins and themes with <code>print_r</code> statements. This plugin displays log messages from PHP in the browser console in Safari and Firefox (with firebug). You no longer have to work blind when your templates don't render properly. Just place a log statement and examine the code right on the browser. ]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.turingtarpit.com/wp-content/uploads/2009/05/wplogger_icon.jpg" alt="wplogger_icon" title="wplogger_icon" width="200" height="200" class="alignleft size-full wp-image-390  post-thumb" /><span class="post-tagline">Essential debugging tool for plugin and theme developers.</span></p>
<p>Display log messages from PHP in the browser console in Safari and Firefox (with firebug). You no longer have to use <code>print_r</code> statements from PHP to figure out what is going on in the code. Besides, <code>print_r</code> statements  mess up the DOM and HTML layout. </p>
<p><a href="http://downloads.wordpress.org/plugin/wordpress-logger.zip"><img class="alignnone size-full wp-image-313" title="Download wplogger plugin" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/download2_btn.jpg" alt="Download wplogger plugin" width="130" height="30" /></a></p>
<a name="wptoc_0_0_0"></a><h3>Features</h3>
<ul>
<li>Log debug messages directly from themes and <span>plugins</span>.</li>
<li>Display log messages in the browser console, without muddying up the browser display.</li>
<li>Displays complex structures such as arrays and objects in pretty print.</li>
<li>Shows the line number and file from where the message was logged ( you won&#8217;t lose track of log statements ).</li>
</ul>
<a name="wptoc_0_0_1"></a><h3><span>Screenshots</span></h3>
<div id="attachment_376" class="wp-caption alignnone" style="width: 310px"><a title="Firefox console showing debug messages." rel="shadowbox[browser console]" href="http://www.turingtarpit.com/wp-content/uploads/2009/05/screenshot-1.png"><img class="size-medium wp-image-376" src="http://www.turingtarpit.com/wp-content/uploads/2009/05/screenshot-1-300x153.png" alt="Firefox console showing debug messages." width="300" height="153" /></a><p class="wp-caption-text">Firefox console showing debug messages.</p></div><br />
<div id="attachment_378" class="wp-caption alignnone" style="width: 310px"><a title="Safari console showing debug messages" rel="shadowbox[browser console]" href="http://www.turingtarpit.com/wp-content/uploads/2009/05/screenshot-2.png"><img class="size-medium wp-image-378   " title="Safari console" src="http://www.turingtarpit.com/wp-content/uploads/2009/05/screenshot-2-300x150.png" alt="Safari console showing debug messages" width="300" height="150" /></a><p class="wp-caption-text">Safari console showing debug messages</p></div>
<a name="wptoc_0_0_2"></a><h3><strong>Installation</strong></h3>
<ol>
<li>Verify that you have PHP5, which is required for this <span>plugin</span>.</li>
<li> <span>Download</span> the whole <code>wplogger</code> folder into the <code>/wp-content/plugins/</code> directory.</li>
<li>Activate the <em>plugin</em> through the <em>Plugins</em> menu in WordPress.</li>
</ol>
<a name="wptoc_0_0_3"></a><h3>Requirements</h3>
<ul>
<li>Make sure that your theme template has a footer ( <em>index.php</em> should have a <em>get_footer()</em> function call at the end).</li>
<li>Turn on the console in your browser:
<ul>
<li><strong>Firefox:</strong> The <a title="Firebug home page" href="http://getfirebug.com/" target="_blank">Firebug</a> extension needs to be installed and activated.</li>
<li><strong>Safari:</strong> Show the <em>Error Console</em> from the <em>Debug</em>/<em>Develop</em> menu.<br />
Details on how to enable the <em>Debug</em> menu for Safari on <a href="http://www.macosxhints.com/article.php?story=20030110063041629" target="_blank">OSX</a> and <a href="http://robrohan.com/2007/06/11/enable-debug-mode-on-safari-windows-error/" target="_blank">Windows</a>.</li>
</ul>
</li>
</ul>
<a name="wptoc_0_0_4"></a><h3>Usage</h3>
<p>After activating the <span>plugin</span>, the following PHP function call can log any PHP expression to the console log.</p>
<div>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$wplogger</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span> php_expression <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> message_type<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<div>
<p>The message_type is optional and can be any one of the following constants:</p>
<ul>
<li>WPLOG_ERR</li>
<li>WPLOG_WARNING</li>
<li>WPLOG_INFO</li>
<li>WPLOG_DEBUG</li>
</ul>
</div>
<a name="wptoc_0_0_5"></a><h3>Use cases</h3>
<a name="wptoc_5_1_0"></a><h4>Logging from template files</h4>
<p>This is from inside the loop to display post IDs.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$wplogger</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Post ID: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[Information: from line 20 in file index.php] Post ID: 125
[Information: from line 20 in file index.php] Post ID: 116
[Information: from line 20 in file index.php] Post ID: 65</pre></div></div>

<a name="wptoc_5_1_1"></a><h4>Logging from PHP files</h4>
<p>This is from  <em>functions.php</em> ( always a good idea to check if <code>$wplogger</code> is available ). Note the message type set to <em>warning</em> through the second parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$wplogger</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$wplogger</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'active_plugins'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> WPLOG_WARNING <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[Warning: from line 55 in file functions.&lt;span&gt;php&lt;/span&gt;] array (
	  0 =&gt; 'wplogger/wplogger.php',
	  1 =&gt; '12seconds-widget/12seconds-widget.php',
	  2 =&gt; 'get-the-image/get-the-image.php',
)</pre></div></div>

<a name="wptoc_5_1_2"></a><h4>Logging from <span>plugins</span></h4>
<p><span>This is from inside a <em>plugin</em> function. Note the global statement to get <code>$wplogger</code> into current scope.</span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wplogger</span><span style="color: #339933;">;</span> <span style="color: #000088;">$wplogger</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'No images attached to this post'</span><span style="color: #339933;">,</span> WPLOG_ERR <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[Error: from line 206 in file get-the-image.php] No images attached to this post</pre></div></div>

<a name="wptoc_0_0_6"></a><h3>Credits</h3>
<p>Code that forces the wplogger plugin to load first was adapted from the WordPress <a title="FirePHP plugin" href="http://inchoo.net/wordpress/wordpress-firephp-plugin/" target="_blank">FirePHP plugin</a> developed by Ivan Weiller.</p>
<p>This plugin is based on <a title="PEAR Log" href="http://pear.php.net/package/Log" target="_blank">PEAR Log</a>, the logging framework that is part of the PHP PEAR library. Current maintainers Jon Parise, Jan Schneider, and Chuck Hagenbuch. PEAR Log is based on code first developed for the Horde 1.3 framework &#8211; original authors Chuck Hagenbuch, and Jon Parise.</p>
<a name="wptoc_0_0_7"></a><h3>Download Plugin</h3>
<p><a href="http://downloads.wordpress.org/plugin/wordpress-logger.zip"><img class="alignnone size-full wp-image-313" title="Download wplogger plugin" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/download2_btn.jpg" alt="Download wplogger plugin" width="130" height="30" /></a></p>
<a name="wptoc_0_0_8"></a><h3>Feature requests and critiques</h3>
<p>This is the first iteration of the wplogger plugin. Please comment on how you would like to see it improved.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.turingtarpit.com/2009/05/wordpress-logger-a-plugin-to-display-php-log-messages-in-safari-and-firefox/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>12seconds Widget: A Plugin to display 12seconds videos on your WordPress blog</title>
		<link>http://www.turingtarpit.com/2009/03/12seconds-widget/</link>
		<comments>http://www.turingtarpit.com/2009/03/12seconds-widget/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 23:12:11 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[Plugin]]></category>
		<category><![CDATA[12seconds]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[social networks]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[video blogging]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.turingtarpit.com/?p=274</guid>
		<description><![CDATA[This is a <strong>WordPress plugin</strong> that adds a <strong>sidebar widget</strong> to display 12seconds video status updates. For those of you who are familiar with Twitter; 12seconds is like Twitter, but with video instead of text updates. You can also embed the widget in <em>posts</em> and <em>pages</em> using <em>shortcodes</em>. The widget itself is essentially a video player. It uses the widget embed code (need to login) provided by <em>12seconds.tv</em>. The plugin saves you the hassle of copying and pasting embed code and editing templates.]]></description>
			<content:encoded><![CDATA[<p>This is a <strong>WordPress plugin </strong>that adds a <strong>sidebar widget</strong> to display <a href="http://12seconds.tv">12seconds</a> video status updates. For those of you who are familiar with Twitter; 12seconds is like Twitter, but with video instead of text updates. You can also embed the widget in <em>posts</em> and <em>pages</em> using <em>shortcodes</em>.</p>
<p><a href="http://downloads.wordpress.org/plugin/12seconds-widget.zip"><img class="alignnone size-full wp-image-233" title="download_btn" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/download2_btn.jpg" alt="Download Link" width="130" height="30" /></a></p>
<p>The widget itself is essentially a video player. It uses the <a href="http://12seconds.tv/widgets" target="_blank">widget embed code</a> (need to login) provided by <em>12seconds.tv</em>. The plugin saves you the hassle of copying and pasting embed code and editing templates.</p>
<a name="wptoc_0_0_0"></a><h3>Installation</h3>
<ol>
<li>Verify that you have PHP5, which is required for this plugin.</li>
<li><a href="http://www.turingtarpit.com/wp-content/uploads/2009/03/12seconds-widget.zip">Download</a> the whole <em>12seconds-widget</em> folder into the <code>/wp-content/plugins/</code> directory.</li>
<li> Activate the plugin through the &#8216;Plugins&#8217; menu in WordPress.</li>
</ol>
<a name="wptoc_0_0_1"></a><h3><strong>Usage</strong></h3>
<p>The plugin can be used in two ways:</p>
<ol>
<li>Display the widget in the sidebar</li>
<li>Embed the widget on posts and pages.</li>
</ol>
<a name="wptoc_1_1_0"></a><h4>1, Displaying the widget on a <em>sidebar</em></h4>
<p>After activating the <em>12seconds-widget</em> plugin, go to the <em>widgets</em> section. Now drag the widget into your chosen <em>sidebar</em> and configure.  You must enter a 12seconds <em>username</em>. You can choose to display a <em>skinny</em> (175&#215;290) or <em>fat</em> (380&#215;440) version of the widget. Make sure you click the &#8216;Done&#8217; button and the &#8216;Save Changes&#8217; button after making changes.</p>
<p><img class="alignnone size-full wp-image-280" title="12seconds-config" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/12seconds-config.jpg" alt="12seconds-config" width="284" height="264" /></p>
<p>If you don&#8217;t want a widget title just leave the <em>Title</em> field blank.</p>
<a name="wptoc_1_1_1"></a><h4>2, Displaying the widget on a <em>post</em> or <em>page</em></h4>
<p>Using the <em>shortcodes</em> system in WordPress 2.5 and up, the plugin will allow you to embed the widget into your <em>pages</em> and <em>posts</em>.</p>
<a name="wptoc_1_2_0"></a><h5>Use the &#8220;12suser&#8221; shortcode to display updates for an individual user</h5>
<p><img class="alignnone size-full wp-image-301" title="12suser-shortcode" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/12suser-shortcode.jpg" alt="12suser-shortcode" width="365" height="104" /></p>
<ul>
<li>The username attribute is required and is the login/user name to the 12seconds site.</li>
<li>The *size* attribute is optional and can be &#8220;fat&#8221; or &#8220;skinny&#8221; (default).</li>
</ul>
<a name="wptoc_1_2_1"></a><h5>Use the &#8220;12svideo&#8221; shortcode to display a single video</h5>
<p><img class="alignnone size-full wp-image-302" title="12svideo-shortcode" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/12svideo-shortcode.jpg" alt="12svideo-shortcode" width="190" height="33" /></p>
<ul>
<li> The <em>id</em> attribute is required.</li>
<li>The <em>id</em> is the last set of digits of the URL when viewing a video on the 12seconds.tv site. The URLs takes the form <code>http://12seconds.tv/channel/username/123456</code> . The <em>id</em> in this case would be <code>"123456"</code>.</li>
</ul>
<p><strong>PS: </strong>The <strong>12s</strong> shortcode introduced in version 0.2 will still work, but it has been deprecated.</p>
<a name="wptoc_0_0_2"></a><h3>Styling Guidelines</h3>
<p>The widget embed code tag is assigned CSS class <code>twelve-s-widget</code>.</p>
<p>The caption for individual videos ( when you use the <code>12svideo</code> shortcode ) is assinged CSS class <code>twelve-s-caption</code> to the tag.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;span class=&quot;twelve-s-caption&quot;&gt;
 ........... &lt;/span&gt;</pre></div></div>

<a name="wptoc_0_0_3"></a><h3>Examples</h3>
<p>The following examples show the status updates and individual videos by the <em>12seconds</em> team.</p>
<iframe class="twelve-s-widget" src="http://embed.12seconds.tv/i/widgetSmall?u=12seconds" scrolling="no" allowtransparency="true" frameborder="0" width="175" height="290"></iframe>
<p>Skinny widget showing the updates for username &#8221;12seconds&#8221;.</p>
<iframe class="twelve-s-widget" src="http://embed.12seconds.tv/i/widgetFull?u=ijustine" scrolling="no" allowtransparency="true" frameborder="0" width="380" height="440"></iframe>
<p>Fat widget showing <a href="http://twitter.com/ijustine" target="_blank">iJustine&#8217;s</a> updates (she&#8217;s not a regular 12er, but the few she did are masterpieces).</p>
<iframe class="twelve-s-widget" src="http://embed.12seconds.tv/i/embed?v=116072" scrolling="no" allowtransparency="true" frameborder="0" width="430" height="360"></iframe><span class="twelve-s-caption"><br/>From <a href="http://12seconds.tv">12seconds.tv</a></span>
<p>Single video showing 12seconds co-founder Sol Lipman explaining what its all about.</p>
<a name="wptoc_0_0_4"></a><h3>Download the plugin</h3>
<p><a href="http://downloads.wordpress.org/plugin/12seconds-widget.zip"><img class="alignnone size-full wp-image-233" title="download_btn" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/download2_btn.jpg" alt="Download Link" width="130" height="30" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.turingtarpit.com/2009/03/12seconds-widget/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Get-the-image-link: Developing a simple WordPress Plugin</title>
		<link>http://www.turingtarpit.com/2009/03/get-the-image-link/</link>
		<comments>http://www.turingtarpit.com/2009/03/get-the-image-link/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 19:41:07 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[Plugin]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.turingtarpit.com/?p=117</guid>
		<description><![CDATA[One of the really cool things about WordPress is that when you want to extend its functionality, more often-than-not, someone has already written a plugin for it.  However, in my case, the available plugin didn't do exactly what I wanted. So, I decided to write my first Wordpress plugin. Here I describe the basic but very important concepts that a novice plugin developer needs to learn.]]></description>
			<content:encoded><![CDATA[<p>One of the really cool things about WordPress is that extending its functionality is a breeze. Think up a new feature and more often-than-not, someone has already written a plugin for it.  With plugin installation now possible within the admin panel &#8211; things couldn&#8217;t be easier. You can search for, download, install and upgrade plugins without leaving the browser. Gone are the days of downloading, unzipping and messing with the command line on a remote server.</p>
<p>So, why did I end up writing a WordPress plugin? It all began with choosing a theme &#8230;.</p>
<a name="wptoc_0_0_0"></a><h3>Choosing a magazine style theme</h3>
<p>I wanted to use a magazine style theme for this blog. I use K2 for all my other WordPress blogs and have been quite happy with it. However, the visual dazzle of <a href="http://wphacks.com/best-magazine-style-wordpress-themes/" target="_blank">recent magazine style themes</a> left K2 a little wanting. Besides, most of K2s unique features like widget control were now standard in WordPress. I was impressed by the <em>Hamasaki</em> theme, developed by Nurudin Jauhari, on <a href="http://aralbalkan.com" target="_blank">Aral Balkan&#8217;s</a> blog. However, the elements were a little too large for my taste and the prominent place given to the most recent post was not to my liking. In contrast, Jauhari had another magazine style theme called <a href="http://themesarena.com/hijriah/" target="_blank"><em>hijiriah</em></a> (<a href="http://demo.themesarena.com/?wptheme=hijriah" target="_blank">demo</a>) that I took a liking to instantly.</p>
<p>There are some excellent features in this theme. The money features for me were:</p>
<ul>
<li>Built-in ad support ( a nice way to link to my other resources ).</li>
<li>Dynamic hierarchical navigation menu based on categories ( structured  access to eclectic subject matter ).</li>
<li>Auto generated image for each post ( pain free way to add some visual interest to the blog page ).</li>
</ul>
<a name="wptoc_0_0_1"></a><h3>Auto generating an image for each post</h3>
<p>I must say that this is the singular feature that really sold me on this theme. Images are generated automatically using a custom version of the <a href="http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin" target="_blank"><em>Get the Image</em></a> plugin developed by Justin Tadlock. This plugin is like a cascading image seeker. It also degrades gracefully. The following image seeking sequence is from the plugin README file.</p>
<ol>
<li>Looks for an image by custom field.</li>
<li>If no image is found, it grabs an image attached to your post.</li>
<li>If no image is attached, it can extract an image from your post content.</li>
<li>If no image is found at this point, it will default to an image you set.</li>
</ol>
<p>The <em>Get the Image</em> plugin has a function called <code>get-the-image</code> that returns the image element ( img tag ) for the auto generated image. A feature that is lacking is flexible image sizing ( you can only choose from predetermined sizes ).  To scale the image to a custom size, the <em>hijriah</em> theme uses an image resizing script called <a href="http://code.google.com/p/timthumb/" target="_blank">timthumb</a>. This is the whole reason for customizing/hacking the plugin.</p>
<p>The custom version of <em>Get the Image</em> that shipped with hijriah was based on version 0.3. The customization was a simple hack to add an additional function to the plugin called <em>get-the-link</em> that returned the URL of the chosen image for the post ( as opposed to the whole image element ). For some reason, a similar function that existed in <em>Get the Image</em> was now deprecated (probably due to a very good reason unknown to me). The URL to the image chosen by the hacked <em>Get the Image</em> plugin was passed to the <em>timthumb</em> script for resizing and eventually displayed on the blog page.</p>
<p>Another issue was that version 0.3 of  <em>Get the Image</em> had a bug that prevented it from returning an image specified by a custom thumbnail field. Version 0.3.1 fixed this issue.</p>
<a name="wptoc_0_0_2"></a><h3>Upgrading plugins broke image auto generation</h3>
<p>Of course, upgrading the <em>Get the Image</em> plugin to 0.3.1 overwrote the hacked version and the <em>hijriah</em> theme was left without an implementation for the template tag <code>get-the-link</code>.</p>
<p>Modifying the code in the <em>hijriah</em> theme ( I was using a child theme ) to substitute <em>get-the-link</em> tag with the <em>get_the_image</em> tag seemed a little too complicated due to the resizing issue. The other option was to write a new plugin that implemented the <code>get-the-link</code> function.</p>
<p>This was a beginning of my sojourn into plugin development&#8230;</p>
<a name="wptoc_0_0_3"></a><h3><strong><em>Writing a plugin to implement the <code>get-the-link</code> </em>template tag</strong></h3>
<p>Template tags are used within WordPress theme templates to display information dynamically. It is really nothing more than a call to a function internal to WordPress or externally implemented in a <em>plugin</em> or <code>functions.php</code> file. In the case of the <em>hijriah</em> theme, the <code>get-the-link</code> tag is required in the <code>index.php</code> template file.</p>
<p>The purpose of the new plugin clear and simple. Implement the <code>get-the-link</code> function by calling the <code>get_the_image</code> function in the  <em>Get the Image</em> plugin. I just have to use regex to extract the <code>src</code> attribute from the image element and return it. Simple right? Well, there were a few important things that I had to learn first.</p>
<a name="wptoc_0_0_4"></a><h3>Important stuff about plugins</h3>
<p>This is my first attempt at writing a plugin for WordPress. I had to dispel some preconceived ideas.</p>
<p>It&#8217;s been awhile since I did any coding in PHP. Coming from my recent ActionScript 3 background and dabbling extensively in OOP and <a href="http://www.as3dp.com" target="_blank">design patterns</a> I thought the new plugin could simply extend the  <em>Get the Image</em> plugin class and override a method or two. But, noooo! I quickly realized what I was up against as my PHP started to come back.</p>
<p>WordPress has a nice modular architecture, but it is not modular in the OOP sense. Here is the definition of a plugin from the <a href="http://codex.wordpress.org/Writing_a_Plugin" target="_blank">WordPress Codex</a>.</p>
<blockquote><p>A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress <a title="Plugin API" href="http://codex.wordpress.org/Plugin_API" target="_blank">Plugin Application Program Interface (API)</a>.</p></blockquote>
<p>Forget about looking for a class to extend. All the plugins I looked at were collections of functions. Here are some things I learned about WordPress plugins that may be useful for a newbie like myself.</p>
<a name="wptoc_4_1_0"></a><h4>The bar to entry is very low</h4>
<p>It was surprising how easy it was to get WordPress to recognize a new plugin. This is a testament to the simplicity of the plugin architecture in WordPress. All you need is to create a new PHP file inside the plugins folder in your WordPress installation and write a comment block on it. Although not a requirement, it is always a good idea to put your plugin files inside a folder. The comment is the <a href="http://codex.wordpress.org/Writing_a_Plugin#File_Headers" target="_blank">plugin information header</a>.</p>
<p><a title="Plugin showing the header block" rel="shadowbox" href="http://www.turingtarpit.com/wp-content/uploads/2009/03/plugin_file.jpg"><img class="alignnon size-full wp-image-208" title="plugin_file" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/plugin_file.jpg" alt="plugin_file" width="542" height="245" /></a></p>
<p>You can now go into the WordPress admin panel and activate it (from the inactive plugins list). You will now see the plugin activated. Of course it doesn&#8217;t do anything, but that was incredibly easy!</p>
<div id="attachment_204" class="wp-caption alignnone" style="width: 310px"><a title="Admin panel showing the activated plugin" rel="shadowbox" href="http://www.turingtarpit.com/wp-content/uploads/2009/03/plugin_admin.jpg"><img class="size-medium wp-image-204   " title="plugin_admin" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/plugin_admin-300x129.jpg" alt="Turing on the plugin from the Admin Panel" width="300" height="129" /></a><p class="wp-caption-text">Activate the plugin from the Admin Panel</p></div>
<a name="wptoc_4_1_1"></a><h4>Plugins are just a collection of functions</h4>
<p>The best way to learn how to write a plugin is by studying other plugins. Good starting point are <em>akismet</em> and the <a href="http://svn.automattic.com/wordpress/trunk/wp-content/plugins/hello.php" target="_blank"><em>hello dolly</em></a> plugins that are included in the default installation. You will quickly see that most plugins have no class definitions, but they are just a collection of functions.</p>
<p>Plugins can take two forms:</p>
<ol>
<li>Plugin as a library.</li>
<li>Plugin as a dependency injector.</li>
</ol>
<a name="wptoc_1_2_0"></a><h5>1. Plugin as library</h5>
<p><span style="font-weight: normal; font-size: 13px;">This is the simplest form of a plugin. A library is simply a collection of functions. The client, a WordPress template in this case, can call the functions. Each function call does some work and returns control to the client.</span></p>
<p><a href="http://codex.wordpress.org/Template_Tags" target="_blank">Template tags</a> are calls to WordPress functions that do something or return some value back to a template. For example,  the <code>bloginfo</code> template tag is a function implemented within WordPress and returns information about the blog based on passed parameters. The &#8216;<code>name</code>&#8216; parameter, shown below,  returns the name of the blog.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Custom template tags can be implemented in plugins. This is a super easy way to extend WordPress functionality. All you need to do is implement a function in your plugin with the name of a template tag you come up with (make sure it is unique &#8211; more on this later). The following figure shows the <code>index.php</code> template calling the <code>get-the-image</code> template tag. The <code>get-the-image</code> function is implemented in the <em>Get the Image</em> plugin.</p>
<p><a href="http://www.turingtarpit.com/wp-content/uploads/2009/03/plugin1.png"><img class="alignncenter size-full wp-image-197" title="plugin1" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/plugin1.png" alt="plugin1" width="414" height="256" /></a></p>
<a name="wptoc_1_2_1"></a><h5><strong>2. Plugin as dependency injector</strong></h5>
<p><strong><span style="font-weight: normal;">This is a more powerful method of extending the functionality of WordPress. Instead of waiting for the template to call the plugin function, the plugin <em>injects</em> the function into the template. Dependency injection using plugins is called <strong>hooks</strong> in WordPress. There are two types of functions that can be injected or hooked into wordpress: <em>Actions</em> and <em>Filters</em>.</span></strong></p>
<p><strong><em>Action</em></strong> functions can be hooked into specific events in WordPress. That means, the function is called or triggered by a specific WordPress event. For example, you can write an action function that informs different search engines when a new article is posted. The  plugin function will hook into the <em>publish_post</em> event. The events that can trigger an action function are numerous. Refer to the <a href="http://codex.wordpress.org/Plugin_API/Action_Reference" target="_blank">Plugin API/Action Reference</a> for the list.</p>
<p><strong><em>Filter</em></strong> functions can be hooked into WordPress  to pass content through the function before being output to the web page. For example, if you want to filter out profanity in comments, write a function that takes the comment text as a parameter and returns the clean content. Now you can hook this filter function to the <em>comment_text</em> filter hook so that it is called before comment text is published on the page. Refer to the <a href="http://codex.wordpress.org/Plugin_API/Filter_Reference" target="_blank">Plugin API/Filter Reference</a> for the list of filter hooks.</p>
<p><a href="http://www.amazon.com/gp/product/1847193595?ie=UTF8&amp;tag=chandimcumara-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=1847193595" target="_blank"><img class="alignleft" style="padding-right: 5px;" src="https://images-na.ssl-images-amazon.com/images/I/51Ns3okgLLL._SL160_.jpg" alt="" width="130" height="160" /></a>I won&#8217;t go into detail on writing plugins as dependency injectors. The <a href="http://codex.wordpress.org/Plugin_API" target="_blank">plugin API documentation</a> provides a good introduction. A good example on how to write <em>action</em> functions is to take a look at the <a href="http://svn.automattic.com/wordpress/trunk/wp-content/plugins/hello.php" target="_blank">hello dolly</a> plugin. The book on <a href="http://www.amazon.com/gp/product/1847193595?ie=UTF8&amp;tag=chandimcumara-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=1847193595" target="_blank">WordPress Plugin Development</a> by Vladimir Prevolac is quite good and explains action and filter functions very well ( check out the <a href="http://www.packtpub.com/files/wordpress-plugin-development-sample-chapter-2-social-bookmarking.pdf" target="_blank">sample chapter</a> ). Above all, download some plugins and take a look at the code &#8211; you will learn some interesting techniques.</p>
<a name="wptoc_4_1_2"></a><h4><strong>Choose plugin and function names carefully</strong></h4>
<p>Make sure you use a unique name for your plugin and its function names. PHP  and WordPress don&#8217;t take kindly to duplicate function names. The plugin will just not load and you will be left wondering what happened. Searching the WordPress <a href="http://wordpress.org/extend/plugins/" target="_blank">plugin directory</a> before settling on a name is not a bad idea. The dash &#8216;-&#8217; is the recommended separator for words in the filenames and function names for plugins.</p>
<a name="wptoc_4_1_3"></a><h4>Make sure the plugin function exists before you call it</h4>
<p>This is also known as:</p>
<blockquote><p>Looking for a black cat in a dark alley that isn&#8217;t there</p></blockquote>
<p>This is most important for library type plugins that are called directly from templates. The template doesn&#8217;t know if the plugin exists or if it is activated. Therefore, it is a good idea to first check if the function is available before calling it from a template. The PHP statement in the template should be of the form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get-the-image'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">-</span>the<span style="color: #339933;">-</span>image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The tag function  &#8217;<code>get-the-link</code>&#8216; will only be called if it exists. There is a tight coupling between the template tag and the plugin where the function is implemented. You need to manually add the tag to the new theme templates if you switch themes.</p>
<p>One of the big advantages of injector type plugins is that the hooks are created at runtime. The template files don&#8217;t have to be modified to call the functions. In fact, the template doesn&#8217;t know anything about the plugins or the hooks. Therefore, themes can be switched without requiring any changes in the template files.</p>
<a name="wptoc_0_0_5"></a><h3>Get the Image Link plugin</h3>
<p>Let&#8217;s get back to the issue at hand. The purpose of the new plugin is to implement the <em>get-the-link</em> template tag by calling the <em>get_the_image</em> function in the  <em>Get the Image</em> plugin. We need to return the URL of the image as opposed to the image element.</p>
<p>It is a good idea to take a look at the <a title="Get the Image README file" rel="shadowbox" href="http://www.turingtarpit.com/wp-content/uploads/2009/03/readme.html">README file</a> for the <em>Get the Image</em> plugin before proceeding. First thing to note is that the function takes <a href="http://codex.wordpress.org/Template_Tags/How_to_Pass_Tag_Parameters#Tags_with_query-string-style_parameters" target="_blank">query string style parameters</a> (as opposed to function style parameters). Query string style is desirable when a function has many parameters. The second issue to note is the &#8216;echo&#8217; parameter which is set to true by default. This causes the function to echo the image element text to the output buffer inside the function. We need to set this to false.</p>
<p>The plugin will consist of one function called <code>get-the-link</code> that calls <code>get-the-image</code> in the <em>Get the Image</em> plugin. It then parses the image element to extract the <code>src</code> attribute using regexp and returns it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_the_link<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_the_image'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> wp_parse_args<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'echo'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prevent echo output</span>
&nbsp;
		<span style="color: #000088;">$imageElement</span> <span style="color: #339933;">=</span> get_the_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/src=(['</span><span style="color: #0000ff;">&quot;])?(.*?)<span style="color: #660099; font-weight: bold;">\1</span>/i';
		preg_match(<span style="color: #006699; font-weight: bold;">$pattern</span>, <span style="color: #006699; font-weight: bold;">$imageElement</span>, <span style="color: #006699; font-weight: bold;">$matches</span>);
&nbsp;
		return <span style="color: #006699; font-weight: bold;">$matches[2]</span>;
	}
}</span></pre></div></div>

<p><span style="font-weight: normal; font-size: 13px;">You can now call the get-the-link template tag from a template and pass it as a parameter to the <a title="timthump.php script for image resizing" href="http://timthumb.googlecode.com/svn/trunk/timthumb.php" target="_blank">timthumb</a> script for resizing. Look at the <code>timthumb.php</code> comment block for parameters and usage. The <code>timthumb.php</code> file is included in the <em>hijriah</em> theme. </span></p>
<p><span style="font-weight: normal; font-size: 13px;">I must say that writing a library type plugin was much simple than I thought. So, don&#8217;t hesitate to give it a try as the more you learn, the more comfortable you will be about customizing WordPress. I learned a lot about theme development during this process and now feel comfortable enough to develop a custom theme. I&#8217;ll write about that experience in a future post.</span></p>
<a name="wptoc_0_0_6"></a><h3>Download the Plugin</h3>
<p><a href="http://www.turingtarpit.com/wp-content/uploads/2009/03/get-the-image-link.zip"><img class="alignnone size-full wp-image-233" title="Download get-the-image-link.zip" src="http://www.turingtarpit.com/wp-content/uploads/2009/03/download_btn.jpg" alt="download_btn" width="101" height="34" /></a></p>
<p><br class="spacer_" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.turingtarpit.com/2009/03/get-the-image-link/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
