<?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; widget</title>
	<atom:link href="http://www.richardshepherd.com/tag/widget/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardshepherd.com</link>
	<description>Making the web. Better looking.</description>
	<lastBuildDate>Fri, 04 Jun 2010 18:03:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to 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>
		<item>
		<title>phpFlickr Widget</title>
		<link>http://www.richardshepherd.com/phpflickr-widget/</link>
		<comments>http://www.richardshepherd.com/phpflickr-widget/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 10:29:10 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Flickr]]></category>
		<category><![CDATA[phpFlickr]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=175</guid>
		<description><![CDATA[You may have noticed a new widget in the sidebar called the phpFlickr Widget. It&#8217;s a widget I have developed for WordPress which allows you to include your Flickr photos in your blog, and display them with the rather funky Slimbox tool. Click on one of the photos and see what I mean. Slimbox, as [...]]]></description>
			<content:encoded><![CDATA[<p>You may have noticed a new widget in the sidebar called the phpFlickr Widget.</p>
<p>It&#8217;s a widget I have developed for WordPress which allows you to include your Flickr photos in your blog, and display them with the rather funky Slimbox tool. Click on one of the photos and see what I mean. Slimbox, as well as looking great, allows you to navigate back and forth if you hover your mouse over the top left and top right of the picture.</p>
<p>The widget itself has a number of configurable options which are easy to set up and change. At the moment they include:</p>
<ul>
<li>API Key</li>
<li>API Secret</li>
<li>Set ID</li>
<li>Number of photos</li>
<li>Photos per row</li>
</ul>
<p>Before I release the widget I&#8217;ll be adding some more options, including the Flickr username and an ability to style how the thumbnails are displayed. At the moment they are just 50px squares.</p>
<p>Stayed tuned for more phpFlickr fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/phpflickr-widget/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
