<?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>Richard Shepherd &#187; tutorial</title>
	<atom:link href="http://www.richardshepherd.com/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardshepherd.com</link>
	<description>Making the web. Better looking.</description>
	<lastBuildDate>Fri, 04 Jun 2010 18:03:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to use jQuery with a JSON Flickr feed to display photos</title>
		<link>http://www.richardshepherd.com/how-to-use-jquery-with-a-json-flickr-feed-to-display-photos/</link>
		<comments>http://www.richardshepherd.com/how-to-use-jquery-with-a-json-flickr-feed-to-display-photos/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:43:29 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Flickr]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.growlingranger.com/?p=427</guid>
		<description><![CDATA[So today I needed to display photos from our Flickr Group without using php or asp.net. I have a WordPress widget that does this job nicely, but no PHP was allowed. Thank goodness for jQuery! jQuery has a handy little function which allows us to load JSON data using an HTTP GET request, and then [...]]]></description>
			<content:encoded><![CDATA[<p>So today I needed to display photos from our Flickr Group without using php or asp.net. I have a WordPress widget that does this job nicely, but no PHP was allowed.</p>
<p>Thank goodness for jQuery! jQuery has a handy little function which allows us to load JSON data using an HTTP GET request, and then process the contents as an array. And with some natty CSS to square things up as a grid, I have nine random (ish) photos from our Flickr group displaying in a nice 3&#215;3 grid.</p>
<p><a href="http://www.richardshepherd.com/flickr.html" target="_blank">See it in action</a>.</p>
<p>The full commented code is at the bottom of this post, but let&#8217;s go through it bit by bit.<span id="more-427"></span></p>
<h2>What is JSON?</h2>
<p>Since we&#8217;re talking about Flickr, let&#8217;s use they&#8217;re definition:</p>
<p><em>&#8220;JSON, or JavaScript Object Notation, is a simple machine-readable data-interchange format, which makes constructing API applications in JavaScript easy (though it can be used from other languages too!). &#8220;</em></p>
<p>Which means what exactly? Well, it&#8217;s a lot like any other feed or data &#8211; like an RSS feed, for example. A typical Flick JSON response might look like this:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Talk On Travel Pool&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;link&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.flickr.com/groups/talkontravel/pool/&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Travel and vacation photos from around the world, featured on the Talk On Travel blog. Join us in sharing your favourite destinations, your top hot spots, and the places to avoid. &quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;modified&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2009-02-02T11:10:27Z&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;generator&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.flickr.com/&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;items&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;View from the hotel&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;link&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.flickr.com/photos/33112458@N08/3081564649/in/pool-998875@N22&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;media&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;m&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;date_taken&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008-12-04T04:43:03-08:00&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&lt;p&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.flickr.com/people/33112458@N08/<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt; Talk On Travel&lt;<span style="color: #000099; font-weight: bold;">\/</span>a&gt; has added a photo to the pool:&lt;<span style="color: #000099; font-weight: bold;">\/</span>p&gt; &lt;p&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http:// www.flickr.com/photos/33112458@N08/3081564649/<span style="color: #000099; font-weight: bold;">\&quot;</span> title=<span style="color: #000099; font-weight: bold;">\&quot;</span>View from the hotel<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt; &lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg<span style="color: #000099; font-weight: bold;">\&quot;</span> width=<span style="color: #000099; font-weight: bold;">\&quot;</span>240<span style="color: #000099; font-weight: bold;">\&quot;</span> height=<span style="color: #000099; font-weight: bold;">\&quot;</span>180<span style="color: #000099; font-weight: bold;">\&quot;</span> alt=<span style="color: #000099; font-weight: bold;">\&quot;</span>View from the hotel<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&lt;<span style="color: #000099; font-weight: bold;">\/</span>a&gt;&lt;<span style="color: #000099; font-weight: bold;">\/</span>p&gt; &quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;published&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008-12-04T12:43:03Z&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;author&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;nobody@flickr.com (Talk On Travel)&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;author_id&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;33112458@N08&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;tags&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;spain dolphins tenerife canaries lagomera aqualand playadelasamericas junglepark losgigantos loscristines talkontravel&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>Which is not altogether too hard to digest. <a href="http://api.flickr.com/services/feeds/groups_pool.gne?id=998875@N22&amp;lang=en-us&amp;format=json&amp;jsoncallback=?" target="_blank">To see the whole feed from Talk On Travel have a look here</a>.</p>
<p>All we need to do is get that feed in a way our javascript can use, and then use it. Enter, stage left, jQuery&#8230;</p>
<h2>Create our object and fill it with data</h2>
<p>So how can jQuery help us out? With the handy <strong>$.getJSON(url,  [data], [callback])</strong> command.</p>
<p>It&#8217;s simpliest method would be something like:</p>
<div class="codecolorer-container javascript default" style="border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.example.com/yourjsonfeed.json&quot;</span><span style="color: #339933;">,</span> functionName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Which would grab the info from a json feed at <em>www.example.com/yourjsonfeed.json</em> and then execute a function called <em>functionName</em>.</p>
<p>Then, you&#8217;d have to create a function like so:</p>
<div class="codecolorer-container javascript default" style="border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> functionName<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>In which <em>data</em> is automatically populated with the json feed. Nice, eh?</p>
<p>So now we have our <em>data</em> object, we need to know how to grab stuff out of it. Again, this is pretty easy stuff. We use some dot syntax like:</p>
<div class="codecolorer-container javascript default" style="border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">data.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">media</span>.<span style="color: #660066;">m</span></div></td></tr></tbody></table></div>
<p>Which says&#8230; in <em>data,</em> go to (the first) <em>item</em>, then go to the <em>media</em> bit and grab the <em>m</em> bit.</p>
<p>Scroll back to the top of this page, and in our example you&#8217;ll see that this would be &#8216;http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg&#8217;.</p>
<p>You can probably work out then what <em>item.title</em> and <em>item.link</em> would give us. Armed with that, you can get any information you need from this feed. All we have to do now is display it!</p>
<h2>Getting this feed onto the screen</h2>
<p>Does that make sense so far? Hope so. If you get lost, check out the excellent jQuery documentation &#8211; the links for which are at the bottom of this post.</p>
<p>Let&#8217;s sew it all together in a really simple function that just cycles through all the images in the feed. By default, Flickr returns 20 images.</p>
<p>As I&#8217;ve commented the code, you should be able to work it out from here&#8230;</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Our very special jQuery JSON fucntion call to Flickr, gets details</span><br />
<span style="color: #006600; font-style: italic;">// of the most recent 20 images</span><br />
<br />
$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://api.flickr.com/services/feeds/groups_pool.gne?id=<br />
998875@N22〈=en-us&amp;amp;format=json&amp;amp;jsoncallback=?&quot;</span><span style="color: #339933;">,</span> displayImages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> displayImages<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Start putting together the HTML string</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> htmlString <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Now start cycling through our array of Flickr photo details</span><br />
&nbsp; &nbsp; $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">items</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// I only want the ickle square thumbnails</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> sourceSquare <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">media</span>.<span style="color: #660066;">m</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;_m.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_s.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Here's where we piece together the HTML</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;li&gt;&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">link</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; target=&quot;_blank&quot;&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;img title=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; src=&quot;'</span> <span style="color: #339933;">+</span> sourceSquare<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&quot; alt=&quot;'</span><span style="color: #339933;">;</span> htmlString <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; /&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;/a&gt;&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; '</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Pop our HTML in the #images DIV</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#images'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>htmlString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Close down the JSON function call</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<h2>But those aren&#8217;t my photos!?</h2>
<p>I know, I know. Have a look at that feed address once more&#8230;</p>
<p><em>http://api.flickr.com/services/feeds/groups_pool.gne?id=998875@N22&amp;lang=en-us&amp;format=json&amp;jsoncallback=?</em></p>
<p>Spot the <em>998875@N22 </em>bit? Well that&#8217;s the Flickr user ID for Talk On Travel, another blog that I look after. You need to put your own ID in that string.</p>
<p>I recommend using  http://idgettr.com/ to find out easily!</p>
<h2>Over to you</h2>
<p>That&#8217;s a simple introduction to JSON and Flickr, and I hope it puts you on the right path.</p>
<p>For more details on JSON and jQuery, and with another example of how to get photos from a Flickr JSON feed, check out <a href="http://docs.jquery.com/Ajax/jQuery.getJSON" target="_blank">http://docs.jquery.com/Ajax/jQuery.getJSON</a>.</p>
<p>To read up on the Flickr JSON object, head over to <a href="http://www.flickr.com/services/api/response.json.html" target="_blank">http://www.flickr.com/services/api/response.json.html</a>.</p>
<p>You can see my code in action at <a href="http://www.richardshepherd.com/flickr.html" target="_blank">http://www.richardshepherd.com/flickr.html</a>. Check out the source code to see what it&#8217;s doing.</p>
<p>Obviously, you can do a lot more with this, but it&#8217;s just a basic implementation to get you started.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/how-to-use-jquery-with-a-json-flickr-feed-to-display-photos/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>How to write a WordPress Twitter Widget &#8211; Part 3 of 3</title>
		<link>http://www.richardshepherd.com/how-to-write-a-wordpress-twitter-widget-part-3-of-3/</link>
		<comments>http://www.richardshepherd.com/how-to-write-a-wordpress-twitter-widget-part-3-of-3/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:12:25 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://www.growlingranger.com/?p=410</guid>
		<description><![CDATA[Welcome to the final part of our tutorial on how to write your own WordPress Twitter Widget. Does this all look a little strange? If so, check out part 1 and part 2 of this tutorial! First, let&#8217;s look at the code&#8230; function WordTweet_control() { &#160; // We need to grab any preset options $options [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the final part of our tutorial on how to write your own WordPress Twitter Widget.<br />
<span id="more-410"></span></p>
<p>Does this all look a little strange? If so, check out <a href="http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-1-of-3/">part 1</a> and <a href="http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-2-of-3/">part 2</a> of this tutorial!</p>
<p>First, let&#8217;s look at the code&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">function WordTweet_control() {
&nbsp;
	// We need to grab any preset options
	$options = get_option(&quot;widget_WordTweet&quot;);
&nbsp;
	// No options? No problem! We set them here.
	if (!is_array( $options )) {
		$options = array(
	  	'WordTweetTitle' =&gt; 'WordTweet',
	  	'username' =&gt; 'Username',
	  	'password' =&gt; 'Password',
	  	'tweets' =&gt; '1'
		);
	}
&nbsp;
	// Is the user has set the options and clicked save,
	// Then we grab them using the $_POST function.
	if ($_POST['WordTweet-Submit']) {
		$options['WordTweetTitle'] =
		  htmlspecialchars($_POST['WordTweet-WidgetTitle']);
		$options['username'] =
		  htmlspecialchars($_POST['WordTweet-WidgetUsername']);
		$options['password'] =
		  htmlspecialchars($_POST['WordTweet-WidgetPassword']);
		$options['tweets'] =
		  htmlspecialchars($_POST['WordTweet-WidgetTweets']);
		// And we also update the options in the Wordpress Database
		update_option(&quot;widget_WordTweet&quot;, $options);
	}
&nbsp;
	?&gt;
	&lt;p&gt;
		&lt;label for=&quot;WordTweet-WidgetTitle&quot;&gt;Title:&lt;/label&gt;&lt;br /&gt;
		&lt;input type=&quot;text&quot;
		  id=&quot;WordTweet-WidgetTitle&quot;
		  name=&quot;WordTweet-WidgetTitle&quot;
		  value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'WordTweetTitle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
		&lt;br /&gt;
		&lt;label for=&quot;WordTweet-WidgetUsername&quot;&gt;Username:&lt;/label&gt;&lt;br /&gt;
		&lt;input type=&quot;text&quot;
		  id=&quot;WordTweet-WidgetUsername&quot;
		  name=&quot;WordTweet-WidgetUsername&quot;
		  value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
		&lt;br /&gt;
		&lt;label for=&quot;WordTweet-WidgetPassword&quot;&gt;Password:&lt;/label&gt;&lt;br /&gt;
		&lt;input type=&quot;text&quot;
		  id=&quot;WordTweet-WidgetPassword&quot;
		  name=&quot;WordTweet-WidgetPassword&quot;
		  value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
		&lt;br /&gt;
		&lt;label for=&quot;WordTweet-WidgetTweets&quot;&gt;No. of Tweets:&lt;/label&gt;&lt;br /&gt;
		&lt;input type=&quot;text&quot;
		  id=&quot;WordTweet-WidgetTweets&quot;
		  name=&quot;WordTweet-WidgetTweets&quot;
		  value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tweets'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
		&lt;input type=&quot;hidden&quot;
		  id=&quot;WordTweet-Submit&quot;
		  name=&quot;WordTweet-Submit&quot;
		  value=&quot;1&quot; /&gt;
	&lt;/p&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> WordTweet_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// These are the Wordpress functions which will register the</span>
	<span style="color: #666666; font-style: italic;">// Widget, and also the control - or options, to you and me.</span>
	register_sidebar_widget<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'WordTweet'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'widget_WordTweet'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	register_widget_control<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'WordTweet'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'WordTweet_control'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;plugins_loaded&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;WordTweet_init&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This really should be nothing new, if you followed the tutorial on how to create a WordPress widget. We&#8217;re simply setting up the options for the Widgets page of the WordPress admin, and saving the options to the database with our <strong>update_option(&#8220;widget_WordTweet&#8221;, $options);</strong> command.</p>
<p>In part two, we added a piece of code to link to an external CSS stylesheet, like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// First we grab our CSS stylesheet. It's a really simple one, but it's good practice to</span>
<span style="color: #666666; font-style: italic;">// keep this in a seperate file.</span>
<span style="color: #b1b100;">echo</span><span style="color: #0000ff;">'&lt;link href=&quot;'</span> <span style="color: #339933;">.</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/wp-content/plugins/WordTweet/WordTweet.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now we just need to take a look at what&#8217;s in the stylesheet.</p>
<pre name="code" class="css">
.WordTweet li {
	display:block;
	background-image:url(twitter.png);
	padding-left: 22px;
	background-repeat:no-repeat;
}
</pre>
<p>By displaying our li elements as a block it get&#8217;s rid of the bullet points which are the default. We set a background image to our Twitter &#8216;t&#8217; (you can grab that in the full sourcecode download), and so we also have to pad the text from the left where this image is by 22px. Finally, we make sure this background isn&#8217;t repeated.</p>
<p>That&#8217;s it! By saving that file as an external CSS file you can have all kinds of fun with it. You might want to add a border-bottom and create a nice underlined effect. Whatever tickles your fancy.</p>
<p>Finally, some homework. This is a pretty basic implementation, and there are lots of things you can add to spice it up for your own site. Maybe you can try the following, all pretty easy with some basic php:</p>
<ul>
<li>Try writing a script to display &#8220;added x hours ago&#8221;, like here on the site.</li>
<li>Try making the &#8216;@&#8217; filtering out an option in the Widget settings.</li>
<li>Try changing the CSS so the Tweets appear in speech bubbles, or add your own Twitter icon in a corner of the widget</li>
</ul>
<p>You can download the full sourcecode, CSS and images from <a href="http://www.richardshepherd.com/downloads/WordTweet.zip" target="_blank">http://www.richardshepherd.com/downloads/WordTweet.zip</a>. And as always if you have any questions please just drop me a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/how-to-write-a-wordpress-twitter-widget-part-3-of-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to write a WordPress Twitter Widget &#8211; Part 2 of 3</title>
		<link>http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-2-of-3/</link>
		<comments>http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-2-of-3/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 15:00:29 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[tweet]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[Wordpress Plugin]]></category>

		<guid isPermaLink="false">http://www.growlingranger.com/?p=363</guid>
		<description><![CDATA[Welcome back! So you should have read through part one of the tutorial, where we left things off with the following line of code: $result = $t -&#62; userTimeline&#40;$id=false, $count = &#40;$options&#91;'tweets'&#93; * 4&#41;, $since = false&#41;; (If that looks strange, make sure you check out part 1 of this tutorial!) We now have our [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome back! So you should have read through part one of the tutorial, where we left things off with the following line of code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$t</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">userTimeline</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tweets'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$since</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(If that looks strange, make sure you <a href="http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-1-of-3/">check out part 1</a> of this tutorial!)</p>
<p>We now have our Tweets stored in <strong>$result</strong> so we can just cycle through them and print them out in our widget. To do that we need the next bit of code.</p>
<p>Rather than explain each line after the code, I have heavily commented it which takes you through the process step-by-step&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// If there are no results, lets pop up a default message</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Sorry, nobody's home! Back in five.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Success! We have Tweets! Let's display 'em!</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// First we grab our CSS stylesheet. It's a really simple one, but it's good practice to</span>
<span style="color: #666666; font-style: italic;">// keep this in a seperate file.</span>
<span style="color: #b1b100;">echo</span><span style="color: #0000ff;">'&lt;link href=&quot;'</span> <span style="color: #339933;">.</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/wp-content/plugins/WordTweet/WordTweet.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// We need a way to count the number of Tweets we display, so let's start a counter at '0'.</span>
<span style="color: #000088;">$iTweets</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The first piece of HTML. The &lt;div&gt; class 'WordTweet' is there for our CSS styling</span>
<span style="color: #666666; font-style: italic;">// The &lt;ul&gt; unordered list is how we'll be displaying each Tweet</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;WordTweet&quot;&gt;&lt;ul&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// For each part of the array, in other words each Tweet, we grab the</span>
<span style="color: #666666; font-style: italic;">// string and call it $tweet</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// We're interested in the text within the Tweet, not the other</span>
<span style="color: #666666; font-style: italic;">// information like date (until later!). Let's grab that and call it $message.</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$tweet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">text</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Now I like to take our any Tweets beginning with '@', as these are replies specifically</span>
<span style="color: #666666; font-style: italic;">// to other people. If you wanted you could take this bit out, but don't forget to remove the</span>
<span style="color: #666666; font-style: italic;">// closing '}'</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;@&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iTweets</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tweets'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// make_clickable is a helpful WordPress function that takes things like email addresses</span>
<span style="color: #666666; font-style: italic;">// and websites in a string of text and turns them into links. Ah, the joy of regular</span>
<span style="color: #666666; font-style: italic;">// expressions!!</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> make_clickable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// I don't display the first, as it's in the header of my theme, so I skip the first Tweet</span>
<span style="color: #666666; font-style: italic;">//by making sure that $iTweet is greater than 0.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iTweets</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// You could take this line out...</span>
<span style="color: #666666; font-style: italic;">// Display the Tweet, nestled in a &lt;li&gt;&lt;/li&gt; tag</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$message</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// ...but you'd need to remove this too!</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// That's one Tweet down, so let's increase $iTweet by 1</span>
<span style="color: #666666; font-style: italic;">// and move on to the next</span>
<span style="color: #000088;">$iTweets</span><span style="color: #339933;">++;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End of the If statement</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End of the foreach loop</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Finish up our HTML output</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End of the if ($result===false) statement</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// And now that last bit of HTML from the WordPress Theme</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$after_widget</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Make sense? Let&#8217;s take a look at two lines in a bit more detail.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span><span style="color: #0000ff;">'&lt;link href=&quot;'</span> <span style="color: #339933;">.</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/wp-content/plugins/WordTweet/WordTweet.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>get_bloginfo</strong> is a handy WordPress function which returns various useful pieces of information. For example, get_bloginfo(&#8216;template_directory&#8217;) returns, you&#8217;ve guessed it, the URL of the template directory.</p>
<p>With <strong>get_bloginfo(&#8216;url&#8217;)</strong> we are grabbing the blog URI which the user has set in <strong>Settings</strong>. We then just add in the path to the .css file, which we know is in the WordTweet directory.</p>
<p>Secondly,</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;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;@&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iTweets</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tweets'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></div></div>

<p>So, <strong>(substr($message,0,1)</strong> is a way of grabbing a sub-string (part of a string) from our string <strong>$message</strong>. We are interested in the first character, because I want to know if it&#8217;s a &#8216;@&#8217;, so we add to parameters &#8217;0,1&#8242; The first parameter tells PHP where to start, and we start with the first character which is in position &#8217;0&#8242;. The second parameter is how many characters we want, in our case just the &#8217;1&#8242;.</p>
<p>We then check to see if this is NOT equal to &#8216;@&#8217; with <strong>!=</strong>. If it&#8217;s NOT &#8216;@&#8217; then it&#8217;s not a Twitter reply and so we can display this Tweet. But we have one more check to make!</p>
<p>The second part of that If statement checks to make sure we are only displaying the number of Tweets the user specified. Initially this was just set as <strong>$options['tweets']</strong>, but I had to change it to <strong>+1</strong>. Why? Because I&#8217;m not displaying the first Tweet (it appears in the header of my site) I need to start on Tweet 1, not Tweet 0 (the first in our array). Which means if I want to display 5 Tweets I actually have to loop through until 5 (rather than 4 &#8211; remember 0 to 4 is FIVE Tweets).</p>
<p>I hope that makes sense, as it&#8217;s rather convoluted. If your blog doesn&#8217;t display Tweets anywhere else, you can remove the <strong>&#038;&#038; ($iTweets < $options['tweets']+1))</strong> bit.</p>
<p>Right, that&#8217;s it for part 2. Part three will finish things off with the options and look at the css. It&#8217;s in the css that we add the Twitter logo. I&#8217;ll also suggest some further reading, and ways you can develop this simple script further.</p>
<p><a href="http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-1-of-3/">Go to part 1</a> | <a href="http://www.richardshepherd.com/how-to-write-a-wordpress-twitter-widget-part-3-of-3/">Go to part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-2-of-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to write a WordPress Twitter Widget &#8211; Part 1 of 3</title>
		<link>http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-1-of-3/</link>
		<comments>http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-1-of-3/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 07:57:44 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[tweet]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://www.growlingranger.com/?p=352</guid>
		<description><![CDATA[So you may have noticed in the sidebar over there on the right there&#8217;s a bunch of my recent Tweets. There are plenty of WordPress Twitter widgets, some better than others, but why not start and write your own? In this three part tutorial we&#8217;ll look at putting together the basic code, and then in [...]]]></description>
			<content:encoded><![CDATA[<p>So you may have noticed in the sidebar over there on the right there&#8217;s a bunch of my recent Tweets. There are plenty of WordPress Twitter widgets, some better than others, but why not start and write your own? In this three part tutorial we&#8217;ll look at putting together the basic code, and then in further episodes we&#8217;ll start getting a little more advanced with some jQuery scripting.</p>
<p>First things first, make sure you read and understand the <a href="http://www.richardshepherd.com/how-to-create-a-wordpress-widget/">How to Create a WordPress Widget Tutorial</a>, as we&#8217;ll be building on that code. In fact, adding in the Twitter code is reasonably straightforward.</p>
<p>Twitter has a API we can use to grab all the details, but our life get easy if we use a prewritten PHP class that does some of the hard work for us. A couple are kicking around out there, but I&#8217;m going to work with the lightweight <a href="http://twitter.slawcup.com/twitter.class.phps">twitterPHP by David Billingham</a>. It doesn&#8217;t include all the functionality of the Twitter API, but it&#8217;s got everything we need to kick off with.</p>
<p><span id="more-352"></span></p>
<p>So, we know how to create a Widget thanks to our earlier tutorial. What we now need to do is:</p>
<ol>
<li>Start with the usual widget code</li>
<li>Send a call off to Twitter with our username and password</li>
<li>Ask Twitter for a certain number of Tweets</li>
<li>Display these Tweets in our widget</li>
<li>Finish up the widget code</li>
<li>Write the code for the WordPress widget options</li>
</ol>
<p>And that&#8217;s it. So, without further ado, I give you WordTweet &#8211; my WordPress Twitter widget!</p>
<h3>WordTweet</h3>
<p>Our code starts off almost identical to the WordPress widget tutorial:</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: #666666; font-style: italic;">/*
Plugin Name: WordTweet
Plugin URI: http://www.richardshepherd.com
Description: A  widget that displays your Twitter Tweets
Author: Richard Shepherd
Version: v0.2
Author URI: http://www.richardshepherd.com
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> widget_WordTweet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// First we grab the Wordpress theme args, which we</span>
	<span style="color: #666666; font-style: italic;">// use to display the widget</span>
	<span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// We also need to make sure we include the Twitter Library</span>
    <span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'twitter.class.php'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Now we sniff around to see if there are</span>
	<span style="color: #666666; font-style: italic;">// any preset options</span>
	<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;widget_WordTweet&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// If no options have been set, we need to set them</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$options</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	  	<span style="color: #0000ff;">'WordTweetTitle'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'WordTweet'</span><span style="color: #339933;">,</span>
	  	<span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Username'</span><span style="color: #339933;">,</span>
	  	<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Password'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'tweets'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span>
	  	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Display the widget!</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$before_widget</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$before_title</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'WordTweetTitle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$after_title</span><span style="color: #339933;">;</span></pre></div></div>

<p>You&#8217;ll spot a couple of changes. First we include the PHP Twitter class, which we will make sure is in the same directory as the widget.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'twitter.class.php'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Secondly we have changed our options code a little bit, to take into account the new options we need.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">// If no options have been set, we need to set them</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$options</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	  	<span style="color: #0000ff;">'WordTweetTitle'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'WordTweet'</span><span style="color: #339933;">,</span>
	  	<span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Username'</span><span style="color: #339933;">,</span>
	  	<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Password'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'tweets'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span>
	  	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next up we use the Twitter class to get our Twitter feed:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Create a new Twitter object and query for direct messages</span>
    <span style="color: #000088;">$t</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> twitter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// This is where we grab the string as $result, from the users</span>
    <span style="color: #666666; font-style: italic;">// timeline, with a $count=1 so we only get the most recent</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$t</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">userTimeline</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tweets'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$since</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So, we create an object <strong>$t</strong> and tell it our username and password which we grabbed from the options a few lines earlier. This is all happening on the server so it&#8217;s relatively secure &#8211; no one will see this info in our sourcecode.</p>
<p>Okay, then we query Twitter with the following options:</p>
<ol>
<li><strong>userTimeline</strong> &#8211; the timeline for the user specified in <strong>$options['username']</strong></li>
<li><strong>$id=false</strong> &#8211; This is optional, and allows us to return the results to the Twitter username or id specified. We don&#8217;t use this option, and hence set it to false.</li>
<li><strong>$count = ($options[&#8216;tweets&#8217; * 4)</strong> &#8211; Stick with me on this on, as the logic is a little convoluted. <strong>$count</strong> is simply the number of Tweets to grab. Now on my homepage I display the three most recent, so you might ask why we don&#8217;t simply put <strong>$count = 3</strong>. Good question! Well, as we&#8217;ll see in the next part of this tutorial I like to remove all my &#8216;@&#8217; Tweets because they are normally directed at just one persona and so not always suitable for my homepage. However, it&#8217;s impossible to know how many &#8216;@&#8217; Tweets there are in any given query until we&#8217;ve actually made the query. Make sense? It might be that my last five Tweets are all good. But it might be that four of them are &#8216;@&#8217; Tweets and so that would only leave me one to display. So, I grab four times the number of Tweets I want, because I&#8217;m pretty sure that if I want to display three Tweets, if I get the last 12 there are probably at least three non &#8216;@&#8217; Tweets in there. Why four times? Why not! It could be <strong>* 5</strong> or <strong>* 10</strong> if you wanted. For me, I&#8217;m happy that <strong>* 4</strong> is enough, although we can also set this as an option if we want.</li>
<li>Finally, we have <strong>$since = false</strong>. We can put a HTTP formatted date in here and that means we&#8217;ll only grab Tweets after that date. For this tutorial we are going to leave that as false, but you might want to introduce a new option to set this to the last day, or the last week.</li>
</ol>
<p>That&#8217;s enough for part one. Make sure you understand what we&#8217;ve done here, and also look through the twitter.class.php and make sense of what David Billingham has done.</p>
<p>In part two of this tutorial we&#8217;ll cycle through our Tweets and display them in our widget. See you then!</p>
<p><a href="http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-2-of-3/">Go to part 2</a> | <a href="http://www.richardshepherd.com/how-to-write-a-wordpress-twitter-widget-part-3-of-3/">Go to part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/write-a-wordpress-twitter-widget-part-1-of-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
