<?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 Widget</title>
	<atom:link href="http://www.richardshepherd.com/tag/wordpress-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 create a WordPress Widget &#8211; Part 2 of 3</title>
		<link>http://www.richardshepherd.com/how-to-create-a-wordpress-widget-part-2-of-3/</link>
		<comments>http://www.richardshepherd.com/how-to-create-a-wordpress-widget-part-2-of-3/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 22:52:09 +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[WordPress Widget]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=273</guid>
		<description><![CDATA[Let&#8217;s recap. So far we have the following code: &#60;?php /* 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 */ &#160; function widget_widgetTutorial&#40;$args&#41; &#123; // First we grab the Wordpress theme args, which we // use to display the [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s recap. So far we have the following code:</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: 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>
&nbsp;
<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 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;">// 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>You should already have read part one, and know what all the above does.</p>
<p>We have two more things to do to get this widget working. Firstly we need to put together our options function, which allows the user to set the variable &#8216;text&#8217; to whatever they want. Secondly we need to &#8216;register&#8217; the Widget with WordPress so that it actually appears as part of your blog.</p>
<p>In this, part 2 of a 3 part tutorial, we&#8217;re going to concentrate on setting up those options. In the third and final installment we will register the widget with WordPress and launch it! <span id="more-273"></span></p>
<h3>Setting Widget Options</h3>
<p>What do we mean by options? Well, in your WordPress Dashboard you are able to access all, and edit some, of your widgets. We now need to create that ability to edit the widget options, which will look a little something like this to the user&#8230;<br />
<img class="aligncenter size-full wp-image-326" title="widget_optionsscreenshot1" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/01/widget_optionsscreenshot11.jpg" alt="widget_optionsscreenshot1" width="289" height="276" /></p>
<p>To set our options, we just need a relatively simple function.</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_control<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// We need to grab 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;">// No options? No problem! We set them here.</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;">// Is the user has set the options and clicked save,</span>
<span style="color: #666666; font-style: italic;">// Then we grab them using the $_POST function.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'widgetTutorial-Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'widgetTutorial-WidgetTitle'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'widgetTutorial-WidgetText'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// And we also update the options in the Wordpress Database</span>
update_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;widget_widgetTutorial&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And inside this function we have some fairly vanilla HTML&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;label for=&quot;widgetTutorial-WidgetTitle&quot;&gt;Widget Title:&lt;/label&gt;
&lt;input type=&quot;text&quot;
id=&quot;widgetTutorial-WidgetTitle&quot;
name=&quot;widgetTutorial-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;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
&nbsp;
&lt;label for=&quot;widgetTutorial-WidgetText&quot;&gt;Widget Text:&lt;/label&gt;
&lt;textarea id=&quot;widgetTutorial-WidgetText&quot;
name=&quot;widgetTutorial-WidgetText&quot;
cols=&quot;30&quot; rows=&quot;4&quot;&gt;
<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;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/textarea&gt;
&nbsp;
&lt;input type=&quot;hidden&quot;
id=&quot;widgetTutorial-Submit&quot;
name=&quot;widgetTutorial-Submit&quot;
value=&quot;1&quot; /&gt;</pre></div></div>

<p>You&#8217;ll spot the cheeky little bit of php in there, <strong><?php echo $options['title'];?></strong> and <strong><?php echo $options['text'];?></strong>, which obviously loads our options into the form fields.</p>
<p>And the final bit of PHP, closing the function with curly braces.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Okay, calm down! Let&#8217;s look at each little bit and piece it together. The first thing we do in the function is see if any options have previously been set.</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;widget_widgetTutorial&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>get_option</strong> is the standard WordPress way to grab options for our widget, which it then loads into an array that we&#8217;re calling (rather inventively) <strong>$options</strong>. The argument for the get_option function is the name of the widget, which in our case is <strong>&#8220;widget_widgetTutorial&#8221;</strong>. We&#8217;ll find out how that name is set a little later, and it can be called whatever you like.</p>
<p>Next up, if there aren&#8217;t any values in that array (which means the options have NOT been set), then let&#8217;s set up a couple of deafult values&#8230;</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: #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>If <strong>$options</strong> is an NOT an array (<strong>!is_array</strong>), let&#8217;s add our own values. <strong>$options['title']</strong> is set to the default value of &#8216;Widget Title&#8217;, and <strong>$options['text']</strong> is set to &#8216;Widget Text&#8217;.</p>
<p>If, on the other hand, <strong>$options</strong> <em>is</em> an array when we need to find out what options have already been set.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'widgetTutorial-Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'widgetTutorial-WidgetTitle'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'widgetTutorial-WidgetText'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
update_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;widget_widgetTutorial&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First we check that the form has already been submitted. You&#8217;ll spot in the HTML a little farther above that we have a hidden field called <strong>&#8216;widgetTutorial-Submit&#8217;</strong>, and at the start of this <strong>if</strong> statement we are simply testing to see if that&#8217;s &#8216;true&#8217; &#8211; in other words, has the user just changed and saved the options.</p>
<p>If it has, it means the user has already submitted some options, and we need to grab em!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><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: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'widgetTutorial-WidgetTitle'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s simple enough. We flush out any unwanted junk with the php command &#8216;<strong>htmlspecialchars</strong>&#8216;, and then we use <strong>$_POST</strong> to grab hold of the values the user entered into the form before pressing submit.</p>
<p>So now we have both the options, as set by the user, in our array. We now just need to update the widget, which we do by relying on another useful WordPress function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">update_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;widget_widgetTutorial&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All we&#8217;re doing is telling WordPress to take the options we have stored in our array <strong>$options</strong> and save them into our widget <strong>widget_widgetTutorial</strong>. WordPress then does the rest for us!</p>
<p><a href="http://www.richardshepherd.com/php/part2.zip" target="_blank">You can download the code from Part 2</a> (Right-click the link, and select &#8220;Save As&#8221;).</p>
<p><a href="http://www.richardshepherd.com/how-to-create-a-wordpress-widget-part-3-of-3/">Head on over to part 3</a> where, finally, this will all come together and your first WordPress Widget will be born!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/how-to-create-a-wordpress-widget-part-2-of-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
