<?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; WordPress</title>
	<atom:link href="http://www.richardshepherd.com/tag/wordpress-web/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>WordPress Custom Fields For Beginners</title>
		<link>http://www.richardshepherd.com/wordpress-custom-fields-for-beginners/</link>
		<comments>http://www.richardshepherd.com/wordpress-custom-fields-for-beginners/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 19:10:32 +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[custom fields]]></category>
		<category><![CDATA[Flickr]]></category>
		<category><![CDATA[get_post_meta]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=721</guid>
		<description><![CDATA[This is a tutorial for beginners, and we were all beginners once. To a beginner I remember custom fields looking quite pointless. I couldn&#8217;t quite grasp what they were for. Then, after building a few WordPress sites, I watched an excellent tutorial from Chris Coyier over at CSS Tricks and it really opened my eyes. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-726" title="fieldsofgold" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/fieldsofgold.jpg" alt="fieldsofgold" width="520" height="250" /></p>
<p>This is a tutorial for beginners, and we were all beginners once.</p>
<p>To a beginner I remember custom fields looking quite pointless. I couldn&#8217;t quite grasp what they were for.</p>
<p>Then, after building a few WordPress sites, I watched an excellent tutorial from Chris Coyier over at CSS Tricks and it really opened my eyes. There&#8217;s a link at the bottom of this post to Chris&#8217;s excellent video series. And that link was generated using a custom field.<br />
<span id="more-721"></span><br />
To whet your appetite, here&#8217;s an example of a search function I <a href="http://www.thet.org" target="_blank">built for THET</a> using custom fields. You can select from three options and then WordPress will search through all the posts custom fields until it finds a match.</p>
<p style="text-align: center;"><a href="http://www.thet.org" target="_blank"><img class="size-full wp-image-733 aligncenter" title="thet-search" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/thet-search.jpg" alt="thet-search" width="216" height="220" /></a></p>
<p style="text-align: left;">That&#8217;s a really powerful way to harness WordPress custom fields, but we&#8217;re going to start with something a little easier.</p>
<h3 style="text-align: left;">The project</h3>
<p style="text-align: left;">In this tutorial, we&#8217;re going to stick with something simple. All you&#8217;ll need is some basic PHP, together with feeling comfortable working with some WordPress files; specifically single.php which you&#8217;ll find in http://your-wordpress-url.com/wp-content/themes/your-theme/single.php.</p>
<p style="text-align: left;">To keep things both easy <em>and</em> hopefully useful, we&#8217;re going to create a couple of custom fields which display a credit and a link to a flickr image. I&#8217;m using it in this post! The image at the top of the field (custom <em>fields</em>, gettit?!) came thanks to a flickr user called innoxiuss. And if you scroll to the bottom of this page you&#8217;ll see a credit that looks like this:</p>
<p><img class="alignnone size-full wp-image-732" title="attribute" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/atrribute.jpg" alt="atrribute" width="218" height="27" /></p>
<p>Well that credit is generated thanks to two custom fields and a little bit of php code.</p>
<h3>What is a WordPress Custom Field?</h3>
<p>Okay. So think of a custom field as a bit of data. It can be anything at all, as long as it&#8217;s an alpha numeric string that can be stored in the WordPress database. So a custom field is not an image, it&#8217;s the URL <em>of</em> the image.</p>
<p>A custom field has two properties, a <strong>name</strong> and a <strong>value</strong>.</p>
<p>For example, the name could be <strong>image </strong>and the value could be <strong>www.yoursite.com/image.jpg</strong>.</p>
<p>But here&#8217;s the thing: <em>it doesn&#8217;t have to make that much sense</em>. The name could be <strong>fruit</strong> and the value could be <strong>apple</strong>. And as pointless as that looks, there are instances where that information could come in useful.</p>
<p>Let&#8217;s take the real world example of the flickr credit on the bottom of this page. There are actually two custom fields in there. The first is the flickr user and the second is the URL of the photo.</p>
<p>Name: <strong>flickr_attribute_url</strong><br />
Value: <strong>http://www.flickr.com/photos/innoxiuss/2665404220/</strong></p>
<p>Name: <strong>flickr_attribute_name</strong><br />
Value: <strong>innoxiuss</strong></p>
<h3>How to add a Custom Field</h3>
<p>Custom fields are added on the Edit Post page, and it looks like this&#8230;</p>
<p><img title="custom-fields-empty" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/custom-fields-empty.jpg" alt="custom-fields-empty" width="787" height="236" /></p>
<p>If you can&#8217;t see it, then scroll to the top and under &#8216;Screen Options&#8217; make sure that &#8216;Custom Fields&#8217; is ticked.</p>
<p>All you have to do is click on the &#8216;Enter new&#8217; link (in blue) and enter a name for your data under &#8216;Name&#8217;.</p>
<p>Next enter your data&#8217;s value under &#8216;Value&#8217;.</p>
<p>Finally, click on the &#8216;Add Custom Field&#8217; button and you&#8217;re done. This is what it might look like&#8230;</p>
<p><img class="alignnone size-full wp-image-723" title="adding-custom-field" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/adding-custom-field.jpg" alt="adding-custom-field" width="765" height="166" /></p>
<p>You can add as many custom fields as you like, and on some posts I have over 20. Here you can see I&#8217;ve added two, making sure that the names are <strong>unique</strong>.</p>
<p><img title="custom-fields-full" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/custom-fields-full.jpg" alt="custom-fields-full" width="766" height="186" /></p>
<p>And that&#8217;s it. You now have two custom fields each containing a snippet of data.</p>
<p>But without rolling up our selves and getting our hands dirty with some PHP, this isn&#8217;t going to do a thing.</p>
<h3>Adding the PHP</h3>
<p>Here&#8217;s the code we&#8217;ll be using. Have a read through and see if it makes sense&#8230;</p>
<div class="codecolorer-container php 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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// Check to see if this data (custom field) exists</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// It does, so let's start building our HTML!</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;flickr_attribute&quot;&gt;'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Grab the name of the flickr user from the custom field</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_name'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$flickr_attribute_name</span> <span style="color: #339933;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Grab the url of the flickr user from the custom field</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_url'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$flickr_attribute_url</span> <span style="color: #339933;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flickr_attribute_url'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Continue with our HTML, using our new variables</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'photo courtesy of &lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_url</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&quot; target=&quot;_blank&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/a&gt; over at flickr'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Close of the DIV and we're done</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;&lt;!-- /.flickr attribute --&gt;'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End of the get_post_meta if statement</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Okay, let&#8217;s take a moment to go through the code.</p>
<p>First of all, make sure you have created the custom fields in your WordPress post, and that you have the names of the fields exactly right. After all, if there&#8217;s no custom field data there&#8217;s nothing to do!</p>
<p>We check for the data in the first line:</p>
<div class="codecolorer-container php 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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>We use the WordPress function <a href="http://codex.wordpress.org/Function_Reference/get_post_meta">get_post_meta</a> to grab the value for &#8216;flickr_attribute_name&#8217;. If there&#8217;s no data there then this statement will be null and the if won&#8217;t run. If there is something in that custom field then the code will carry on executing.</p>
<p>But hold on a minute, what&#8217;s all that other stuff? Well, the get_post_meta function has the following parameters:</p>
<div class="codecolorer-container php 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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We set our <strong>$post_id</strong> as the current post using $post->ID. Our <strong>$key</strong> is the name of the custom field which is &#8216;flickr_attribute_name&#8217;. And finally we set <strong>$single</strong> to true.</p>
<p>What is single? If it&#8217;s set to <strong>true</strong> it returns a single result as a string. If it&#8217;s set to <strong>false</strong> it returns multiple results as an array. We&#8217;ll get on to arrays in later tutorials, but for now we&#8217;re going to keep it simple. We only have one value for our flickr credit so we leave it as <strong>true</strong>.</p>
<p>So now we know we have some data, let&#8217;s carry on with the code. We put our content in a DIV so we can style it to our liking</p>
<div class="codecolorer-container php 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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// It does, so let's start building our HTML!</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;flickr_attribute&quot;&gt;'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>And we also create two variables with our custom field data in. The <strong>get_post_meta</strong> function works exactly as we described it above.</p>
<div class="codecolorer-container php 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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Grab the name of the flickr user from the custom field</span><br />
<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_name'</span><br />
<span style="color: #000088;">$flickr_attribute_name</span> <span style="color: #339933;">=</span><br />
<br />
&nbsp; get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Grab the url of the flickr user from the custom field</span><br />
<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_url'</span><br />
<span style="color: #000088;">$flickr_attribute_url</span> <span style="color: #339933;">=</span><br />
&nbsp; get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flickr_attribute_url'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Again, note that we set the <strong>$single</strong> parameter to <strong>true</strong>, because we know there is only one bit of data. How do we know this? Well, in this instance it&#8217;s because we&#8217;ve set the custom field up ourselves in the WordPress backend.</p>
<p>Finally, we throw together the rest of the HTML, and close off the <strong>if</strong> statement.</p>
<div class="codecolorer-container php 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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Continue with our HTML, using our new variables</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'photo courtesy of &lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_url</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&quot; target=&quot;_blank&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/a&gt; over at flickr'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Close of the DIV and we're done</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;&lt;!-- /.flickr attribute --&gt;'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End of the get_post_meta if statement</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<h3>Bringing it together</h3>
<p>So we have our custom fields in WordPress and we&#8217;ve entered some data into them. We&#8217;ve also created the snippet of easy PHP which&#8217;ll read this data and display it on the page.</p>
<p>But where does it go?</p>
<p>Well, for this snippet I&#8217;ve chosen to put it on the bottom of the <strong>single.php</strong> page in the wp-content folder, as this is the page that displays single posts.</p>
<p>On my page it comes just before the <tags> section, which itself is just before the <strong>comments</strong> bit. But you could put it anywhere you want.</p>
<p>Just boot up your favourite PHP editor (I like Expresso on the Mac and, predictably, Dreamweaver on the PC) and type in the code.</p>
<p>Save your page, upload, and you should have a lovely flickr photo credit.</p>
<h3>What next?</h3>
<p>Don&#8217;t have flickr photos on your page? Well then this could be a bit pointless. But remember, this tutorial should just give you a basic idea of how custom fields work. Sure, you could have put this flickr information in the post itself but as we&#8217;ll see, we can do all kinds of funky things once we have the ability to search custom fields and display data based on their contents.</p>
<p>In the mean time there is some further reading below. Oh, and the further reading is displayed from data in custom fields!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/wordpress-custom-fields-for-beginners/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WordPress, IIS, Permalinks and index.php</title>
		<link>http://www.richardshepherd.com/wordpress-iis-permalinks-and-index-php/</link>
		<comments>http://www.richardshepherd.com/wordpress-iis-permalinks-and-index-php/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 18:12:35 +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[.htaccess]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[index.php]]></category>
		<category><![CDATA[isapi]]></category>
		<category><![CDATA[permalinks]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=592</guid>
		<description><![CDATA[Here&#8217;s the problem: You need to install WordPress on a Windows machine running IIS. I&#8217;d never normally do this, but sometimes you just don&#8217;t have a choice. There are plenty of links out there about how to do this, and the one I referred to most was How To Install WordPress on IIS 6.0. It [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the problem:</p>
<p>You need to install WordPress on a Windows machine running IIS. I&#8217;d never normally do this, but sometimes you just don&#8217;t have a choice.</p>
<p>There are plenty of links out there about how to do this, and the one I referred to most was <a href="http://www.iisadmin.co.uk/?p=6" target="_blank">How To Install WordPress on IIS 6.0</a>. It is, more or less, straightforward.</p>
<p>However, there&#8217;s an issue with IIS and permalinks.<span id="more-592"></span>The issue is that if you want to take advantage of the WordPress pretty permalinks, you have to suffer a folder in the path called &#8216;index.php&#8217;. So, instead of the rather lovely:</p>
<p><strong>http://www.yourblog.com/yourcategories/the-best-post-in-the-world/</strong></p>
<p>you have to grit your teeth and accept:</p>
<p><strong>http://www.yourblog.com/index.php/yourcategories/the-best-post-in-the-world/</strong></p>
<p>I don&#8217;t know why, I really don&#8217;t. I&#8217;m not that smart. But I did work out how to fix it. It&#8217;s easy enough, but you have to put a couple of rules in the ISAPI ReWrite Engine to deal with the quirks.</p>
<p>First we need to head into our WordPress admin tool, and go to Settings &gt; Permalinks. There, you&#8217;ll see the craft insertion of /index.php/ in the links. We need to create a custom permalink and <em>delete</em> the index.php bit. Just like this&#8230;</p>
<p><img class="alignnone size-full wp-image-598" title="WordPress Permalink Options" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/07/links1.jpg" alt="WordPress Permalink Options" width="504" height="373" /></p>
<p>Now save that off and head over to your .htaccess file &#8211; we need to weave some magic!</p>
<p>At the bottom of this post is the whole file, but the two lines I particularly want to point out are:</p>
<div class="codecolorer-container c 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 /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_URI<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!/</span>wp<span style="color: #339933;">-</span>admin<br />
RewriteRule <span style="color: #339933;">^/</span><span style="color: #009900;">&#40;</span>.<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>$ <span style="color: #339933;">/</span>index\.<span style="color: #202020;">php</span><span style="color: #339933;">/</span>$<span style="color: #0000dd;">1</span> <span style="color: #009900;">&#91;</span>NC<span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<p>The second line works the juju. It takes the URL from the browser, shoves an /index.php/ into it, and displays the contents. For more on how and why this works, check out <a href="http://www.workingwith.me.uk/articles/scripting/mod_rewrite" target="_blank">http://www.workingwith.me.uk/articles/scripting/mod_rewrite</a> for the basics.</p>
<p>Now the first line is <em>really</em> important, because it tells the server <strong>not</strong> to do this if we&#8217;re in the admin section. If we remove this line, there are all kinds of problems reaching /wp-admin/index.php. Trust me.</p>
<p>I have two other lines in the following code which get rid of index.php from URLS, which stops the google duplicate content issue. They&#8217;re always worth having.</p>
<p>Finally, make sure that wherever your site is hosted, that the correct permissions are set on the wp-admin and wp-content directories and their sub-directories. You must have read/write/modify permissions set for internal user accounts.</p>
<p>If you implement these small tweaks, then your WordPress IIS install should work like a charm.</p>
<p>Good luck!</p>
<p>Here&#8217;s the code:</p>
<div class="codecolorer-container c 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 />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<span style="color: #339933;"># Kickstart the Rewrite Engine and set initial options</span><br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<br />
RewriteEngine On<br />
RewriteCompatibility2 On<br />
RepeatLimit <span style="color: #0000dd;">200</span><br />
RewriteBase<br />
<br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<span style="color: #339933;"># WORDPRESS, WINDOWS &amp; ISAPI DOCUMENTATION</span><br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<span style="color: #339933;"># These are the WordPress redirects we need in place for a Windows Server</span><br />
<span style="color: #339933;"># running PHP &amp; MySQL</span><br />
<span style="color: #339933;"># We had some issues configuring ISAPI with WordPress and so here are the</span><br />
<span style="color: #339933;"># solutions in case we need them again!!</span><br />
<br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<span style="color: #339933;"># PERMISSIONS SETTINGS</span><br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<span style="color: #339933;"># The following folders:</span><br />
<span style="color: #339933;"># wp-admin</span><br />
<span style="color: #339933;"># wp-content</span><br />
<span style="color: #339933;"># MUST HAVE read/write/modify permissions set for internal user accounts</span><br />
<br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<span style="color: #339933;"># ESSENTIAL WORDPRESS REWRITES</span><br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<br />
<span style="color: #339933;"># Redirects 'www.yourblogsite.com/index.php' to 'www.yourblogsite.com/'</span><br />
<br />
RewriteRule <span style="color: #339933;">^/</span>index\.<span style="color: #202020;">php</span>$ <span style="color: #339933;">/</span> <span style="color: #009900;">&#91;</span>NC<span style="color: #339933;">,</span>P<span style="color: #339933;">,</span>R<span style="color: #339933;">=</span><span style="color: #0000dd;">301</span><span style="color: #009900;">&#93;</span><br />
<br />
<span style="color: #339933;"># Rewrite 'www.yourblogsite.com/anything/' to 'www.yourblogsite.com/index.php/anything/'</span><br />
<span style="color: #339933;"># NB. The user does not see this rewrite.</span><br />
<span style="color: #339933;"># Must also go into WordPress &gt; Settings &gt; Permalinks and select 'custom'</span><br />
<span style="color: #339933;"># and then REMOVE 'index.php' from the custom URL it generates</span><br />
<br />
<span style="color: #339933;"># The first line is a condition so it doesn't apply the rule to the wp-admin part of the site</span><br />
<br />
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_URI<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!/</span>wp<span style="color: #339933;">-</span>admin<br />
RewriteRule <span style="color: #339933;">^/</span><span style="color: #009900;">&#40;</span>.<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>$ <span style="color: #339933;">/</span>index\.<span style="color: #202020;">php</span><span style="color: #339933;">/</span>$<span style="color: #0000dd;">1</span> <span style="color: #009900;">&#91;</span>NC<span style="color: #009900;">&#93;</span><br />
<br />
<span style="color: #339933;"># Finally, redirect 'www.yourblogsite.com/anything/anything/index.php'</span><br />
<span style="color: #339933;"># to 'www.yourblogsite.com/anything/anything/'</span><br />
<br />
RewriteRule <span style="color: #339933;">^/</span><span style="color: #009900;">&#40;</span>.<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>index\.<span style="color: #202020;">php</span>$ <span style="color: #339933;">/</span>$<span style="color: #0000dd;">1</span><span style="color: #339933;">/</span> <span style="color: #009900;">&#91;</span>NC<span style="color: #339933;">,</span>P<span style="color: #339933;">,</span>R<span style="color: #339933;">=</span><span style="color: #0000dd;">301</span><span style="color: #009900;">&#93;</span><br />
<br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span><br />
<span style="color: #339933;"># END OF ESSENTIAL REWRITES</span><br />
<span style="color: #339933;"># -------------------------------------------------------------------------</span></div></td></tr></tbody></table></div>
<p>Thanks for watching, kids&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/wordpress-iis-permalinks-and-index-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to remove the smiley face from a WordPress blog</title>
		<link>http://www.richardshepherd.com/how-to-remove-the-smiley-face-from-your-wordpress-blog/</link>
		<comments>http://www.richardshepherd.com/how-to-remove-the-smiley-face-from-your-wordpress-blog/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 19:33:04 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[smiley]]></category>
		<category><![CDATA[smiley face]]></category>
		<category><![CDATA[wordpress stats]]></category>

		<guid isPermaLink="false">http://www.growlingranger.com/?p=441</guid>
		<description><![CDATA[So a little smiley face appears at the bottom of your blog, and it looks like this: Well ain&#8217;t that the cutest thing! But what is it? It&#8217;s the little .gif file that the WordPress Stats plug-in uses to monitor your page. It does so with some code like this&#8230; 1&#60;img src=&#34;http://stats.wordpress.com/g.gif?host=www.yoursite.com&#38;rand=###############&#38;blog=#######&#38;v=###&#38;post=#&#38;ref=#&#34; alt=&#34;&#34; /&#62;; &#8230;where [...]]]></description>
			<content:encoded><![CDATA[<p>So a little smiley face appears at the bottom of your blog, and it looks like this:</p>
<p><img class="alignnone noborder" title="WordPress Stats Smiley Face!" src="http://stats.wordpress.com/g.gif" alt="" width="6" height="5" /></p>
<p>Well ain&#8217;t that the cutest thing! But what is it?</p>
<p>It&#8217;s the little .gif file that the WordPress Stats plug-in uses to monitor your page. It does so with some code like this&#8230;</p>
<div class="codecolorer-container html4strict 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="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stats.wordpress.com/g.gif?host=www.yoursite.com&amp;rand=###############&amp;blog=#######&amp;v=###&amp;post=#&amp;ref=#&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>;</div></td></tr></tbody></table></div>
<p>&#8230;where the #&#8217;s are various characters. It&#8217;s all mystic WordPress voodoo which you don&#8217;t need to worry about.</p>
<p>Now I actually like the smiley face, but some people want to remove it from their blog. And it&#8217;s easy to do.</p>
<p>In wp-admin, head over to <strong>Appearance &gt; Editor &gt; style.css </strong>(or whatever your main style sheet is named). You then just add this bit of CSS:</p>
<div class="codecolorer-container css 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 /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* Let's remove that Wordpress Stats Smiley Face ! */</span><br />
img<span style="color: #cc00cc;">#wpstats</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>There you have it! We&#8217;re just using plain and simple css to remove the smiley face from the screen, but still loading it so the page view gets counted.</p>
<p>So now you know how to remove the WordPress Stats smiley face. The question is, do you really want to?!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/how-to-remove-the-smiley-face-from-your-wordpress-blog/feed/</wfw:commentRss>
		<slash:comments>9</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>
		<item>
		<title>How to create a WordPress Widget &#8211; Part 1 of 3</title>
		<link>http://www.richardshepherd.com/how-to-create-a-wordpress-widget/</link>
		<comments>http://www.richardshepherd.com/how-to-create-a-wordpress-widget/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 22:30:47 +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[widget]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=237</guid>
		<description><![CDATA[So how do you put together your own WordPress widget? All you need is to know your way around PHP. If you can program your application in PHP the chances are it can be turned into a Widget. I&#8217;m first going to go through the structure of a Widget, and then we&#8217;re going to get [...]]]></description>
			<content:encoded><![CDATA[<p>So how do you put together your own WordPress widget? All you need is to know your way around PHP. If you can program your application in PHP the chances are it can be turned into a Widget.</p>
<p>I&#8217;m first going to go through the structure of a Widget, and then we&#8217;re going to get our hands dirty with some code.</p>
<p>A basic Widget has four main components:</p>
<ol>
<li>The Widget Information</li>
<li>The Widget Itself</li>
<li>Any options for the Widget</li>
<li>Initialising/Registering the Widget</li>
</ol>
<p><strong>Widget Header<br />
</strong>The Widget Information is just a bit of information at the top of the file which WordPress uses to describe the Widget. It might look like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
Plugin Name: A Simple WordPress Widget Tutorial
Plugin URI: http://www.richardshepherd.com
Description: Simple Widget Tutorial from RichardShepherd.com
Author: Richard Shepherd
Version: v1.0
Author URI: http://www.richardshepherd.com
*/</span></pre></div></div>

<p>Everything there is fairly self explanatory. The URI (Uniform Resource Identifier) is just a fancy (<a href="http://en.wikipedia.org/wiki/Uniform_Resource_Identifier" target="_blank">and more correct</a>) way of describing the URL. Don&#8217;t worry too much about that for now.<span id="more-237"></span></p>
<p><strong>Widget Functions</strong><br />
After that header, we have three functions to write.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> widgetTutorial<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> widgetTutorial_control<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> widgetTutorial_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first function is the Widget itself, the second is where we set and store the options, and the third kicks the whole thing off.</p>
<p>So, what can be in a Widget? Well anything you can write in PHP. Perhaps is a fun new application you&#8217;ve been tinkering with, or maybe just a way of displaying random photos. Whatever it is, we need to write the code and pop it in the WidgetTutorial() function.</p>
<p><strong>Writing the Widget<br />
</strong>For our purposes we are going to display a header and some text. Both items can be edited from the Manage > Widgets page in your wp-admin. This is a pretty pointless Widget, but hopefully it&#8217;ll open the doors to some fun development for you.</p>
<p>Our Widget function could simply be&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> widgetTutorial<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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>
  <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: #0000ff;">'Hello World!'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Our Title</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$after_title</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'This is the main Widget text.'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// The text.</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>You should be familiar with things like <strong>echo &#8216;Hello World&#8217;;</strong> but what are all those other bits and bobs?</p>
<p>Well, imagine that WordPress has hundreds upon hundreds of Themes. We need to make sure that your Widget will integrate properly with each and every one of them. When some talented designer puts together their theme, they include CSS definitions for Sidebar Widgets and, thanks to the magic of WordPress, we can access them easily. In fact, we don&#8217;t even have to see them. All we need is to drop in</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><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></pre></div></div>

<p>and then we&#8217;ll grab the relevant variables from the $args array. In our case, there are four variables we&#8217;re interested in:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$before_widget</span><span style="color: #339933;">,</span> <span style="color: #000088;">$before_title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$after_title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$after_widget</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can even see that their order mirrors HTML tags. You could almost imagine them written as:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>widget<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>widget<span style="color: #339933;">&gt;</span></pre></div></div>

<p>although of course that wouldn&#8217;t work at all!! So, we extract those variables which go on to define the layout and then start to put together the Widget using echo commands. It really is that simple.</p>
<p><strong>Dynamic = Interesting </strong><br />
But that&#8217;s a static Widget which is, quite frankly, boring. What we&#8217;re interested in is setting up some options to make this puppy a bit more dynamic. Start introducing a bit of randomness, and you have a truly dynamic page &#8211; no two views are the same. So let&#8217;s have a look at how that Widget function changes to accommodate our new options:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> widget_widgetTutorial<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// First we grab the Wordpress theme arguements,</span>
<span style="color: #666666; font-style: italic;">// which we'll 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;">// 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_widgetTutorial&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;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Widget Title'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Widget Text'</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;">'title'</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>
<span style="color: #666666; font-style: italic;">//Our Widget Content</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</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>It&#8217;s really not that different. All we&#8217;ve added is</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;widgetTutorial&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Widget Title'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Widget Text'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</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;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>First we create an array called <strong>$options</strong>, and populate with our Widget options (we&#8217;ll learn how to set these later). <strong>get_option() </strong>is a WordPress function which returns all the options into our array.</p>
<p>Next we test to see if the array <strong>$options</strong> is empty. If it is, it means no options have been set and we have to make up some default values. We do that with <strong>if (!is_array( $options ))</strong>. Where the <strong>!</strong> changes &#8216;is array?&#8217; to &#8216;is not array?&#8217;. If it isn&#8217;t, we create the array in the standard way and pop in some default variables. That&#8217;s fairly straightforward.</p>
<p>All we do next is output parts of the array in our widget, using <strong>echo $options['title'];</strong> and <strong>echo $options['text']; </strong>where &#8216;title&#8217; and &#8216;text&#8217; are the options we have previously set.</p>
<p>Make sense? I hope so. <a href="http://www.richardshepherd.com/php/part1.zip" target="_blank">You can download the code from Part 1</a> (Right-click the link, and select &#8220;Save As&#8221;).</p>
<p>In <a href="http://www.richardshepherd.com/how-to-create-a-wordpress-widget-part-2-of-3/">part two of this tutorial</a>, we&#8217;ll look at putting together the options screen. See you soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/how-to-create-a-wordpress-widget/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
