<?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>Dan Polant :: Web Interaction Designer &#187; sidebar</title>
	<atom:link href="http://danpolant.com/tag/sidebar/feed/" rel="self" type="application/rss+xml" />
	<link>http://danpolant.com</link>
	<description></description>
	<lastBuildDate>Tue, 01 Nov 2011 03:55:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to: Make an element a widget sidebar</title>
		<link>http://danpolant.com/how-to-make-an-element-a-widget-sidebar/</link>
		<comments>http://danpolant.com/how-to-make-an-element-a-widget-sidebar/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 03:25:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=80</guid>
		<description><![CDATA[Widgets are a great way to let people include something cool a bunch of times in their site. The problem is, widget areas are almost always predefined in themes, and usually confined to sidebar areas. So here&#8217;s how to make any html element into a widget container.
What exactly does this mean? It means that after [...]]]></description>
			<content:encoded><![CDATA[<p>Widgets are a great way to let people include something cool a bunch of times in their site. The problem is, widget areas are almost always predefined in themes, and usually confined to sidebar areas. So here&#8217;s how to make any html element into a widget container.</p>
<p>What exactly does this mean? It means that after we&#8217;re done, you will have something on the right of your appearance->widgets screen like:</p>
<div id="attachment_81" class="wp-caption aligncenter" style="width: 189px"><img class="size-full wp-image-81" title="New widget area" src="http://danpolant.com/wp-content/uploads/2009/10/widget-backend.PNG" alt="&quot;Groups Sidebar&quot; is not a default Buddypress widget" width="179" height="85" /><p class="wp-caption-text">&quot;Groups Sidebar&quot; is not a default widget</p></div>
<h4>There&#8217;s nothing special about code that runs inside a widget.</h4>
<p> A widget is similar to any other piece of template code, except that widget output is defined by the return value of a <a href="http://codex.wordpress.org/WordPress_Widgets_Api#Developing_Widgets_on_2.8.2B">special function and class</a>. When you put your custom code into the specified parts of this function, WordPress will treat that code as something that can be run and displayed in a specific widget area, depending on where you drag your widget in /wp-admin/widgets.php.  </p>
<p>Usually you want to define a widget area in your theme file. Lets say we want to add a widget area to the groups directory page (/directories/groups/index.php) in Buddypress. First find a place where you want to put your widget area div. For my example, I&#8217;m going to make a new div in /directories/groups/index.php. You can make a new one like I&#8217;m doing, or use an existing element in the template. I&#8217;ll put my new element right about here:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;sidebar&quot;</span> <span class="kw2">class</span>=<span class="st0">&quot;directory-sidebar&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> do_action<span class="br0">&#40;</span> <span class="st0">&#8216;bp_before_directory_groups_search&#8217;</span> <span class="br0">&#41;</span> <span class="kw2">?&gt;</span>&nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;groups-directory-search&quot;</span> <span class="kw2">class</span>=<span class="st0">&quot;directory-widget&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h3&gt;&lt;?php _e<span class="br0">&#40;</span> <span class="st0">&#8216;Find Groups&#8217;</span>, <span class="st0">&#8216;buddypress&#8217;</span> <span class="br0">&#41;</span> ?&gt;&lt;/h3&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> bp_directory_groups_search_form<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">?&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> do_action<span class="br0">&#40;</span> <span class="st0">&#8216;bp_directory_groups_search&#8217;</span> <span class="br0">&#41;</span> <span class="kw2">?&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> do_action<span class="br0">&#40;</span> <span class="st0">&#8216;bp_after_directory_groups_search&#8217;</span> <span class="br0">&#41;</span> <span class="kw2">?&gt;</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;groups-sidebar&quot;</span>&gt;&lt;!&#8211; my <span class="kw2">new</span> widget area will be element &#8211;&gt;</div>
</li>
</ol>
</div>
<p>This will make the area below the group search box widget friendly. Add the following lines of code underneath, like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;groups-sidebar&quot;</span>&gt;&lt;!&#8211; my <span class="kw2">new</span> widget area starts here &#8211;&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> <span class="kw1">if</span> <span class="br0">&#40;</span> !<a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span><span class="st0">&#8216;dynamic_sidebar&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || !dynamic_sidebar<span class="br0">&#40;</span><span class="st0">&#8216;groups-sidebar&#8217;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> : <span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p><small>see <a href="http://codex.wordpress.org/WordPress_Widgets_Api/dynamic_sidebar">http://codex.wordpress.org/WordPress_Widgets_Api/dynamic_sidebar</a></small></p>
<p>Basically this is saying &#8220;if nothing has called dynamic sidebar on the element with id &#8216;groups-sidebar,&#8217; execute the code that comes after this. This is how you add default sidebar content to display when users have not added any widgets</p>
<p>There is one more step to defining a widget area. You might want to do this in a simple plugin:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">add_action<span class="br0">&#40;</span><span class="st0">&#8216;init&#8217;</span>, <span class="st0">&#8216;custom_sidebar&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> custom_sidebar<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$args</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span>__<span class="br0">&#40;</span><span class="st0">&#8216;Sidebar %d&#8217;</span><span class="br0">&#41;</span>, <span class="re0">$i</span> <span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;id&#8217;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; <span class="st0">&#8216;sidebar-$i&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;before_widget&#8217;</span> =&gt; <span class="st0">&#8216;&lt;li id=<span class="es0">\&#8221;</span>%1$s<span class="es0">\&#8221;</span> class=<span class="es0">\&#8221;</span>widget %2$s<span class="es0">\&#8221;</span>&gt;&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;after_widget&#8217;</span> &nbsp;=&gt; <span class="st0">&#8216;&lt;/li&gt;&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;before_title&#8217;</span> &nbsp;=&gt; <span class="st0">&#8216;&lt;h2 class=<span class="es0">\&#8221;</span>widgettitle<span class="es0">\&#8221;</span>&gt;&#8217;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;after_title&#8217;</span> &nbsp; =&gt; <span class="st0">&#8216;&lt;/h2&gt;&#8217;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; register_sidebar<span class="br0">&#40;</span><span class="re0">$args</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><small>source: <a href="http://codex.wordpress.org/Function_Reference/register_sidebar">http://codex.wordpress.org/Function_Reference/register_sidebar</a></small></p>
<p>You can modify these tags, but WordPress suggests that you leave the sprintf stuff alone for the sake of consistency. </p>
<h4>Your widget area should now show up in wp-admin/widgets.php</h4>
<p>It should display a properly formatted widget where ever you have it located. I found this process especially useful while editing Buddypress, because it seems like the groups and members pages should really be made widget friendly. </p>
<p>I suggest you read the documentation in <a href="http://codex.wordpress.org/WordPress_Widgets_Api">http://codex.wordpress.org/WordPress_Widgets_Api</a> for more information &#8211; this is basically how I learned to do this. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-make-an-element-a-widget-sidebar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

