<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>WordPress Bits &#187; Widgets</title>
	<atom:link href="http://wpbits.wordpress.com/category/wordpress/widgets/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpbits.wordpress.com</link>
	<description>Hacking WordPress. Keeping the bits together.</description>
	<lastBuildDate>Tue, 18 Dec 2007 07:46:40 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='wpbits.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/cb9a73de53854d3a658a4a104c191a09?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>WordPress Bits &#187; Widgets</title>
		<link>http://wpbits.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://wpbits.wordpress.com/osd.xml" title="WordPress Bits" />
		<item>
		<title>Cleaning up after the WordPress widget party</title>
		<link>http://wpbits.wordpress.com/2007/08/11/cleaning-up-after-the-wordpress-widget-party/</link>
		<comments>http://wpbits.wordpress.com/2007/08/11/cleaning-up-after-the-wordpress-widget-party/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 19:26:56 +0000</pubDate>
		<dc:creator>Leonid Mamchenkov</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Widgets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpbits.wordpress.com/2007/08/11/cleaning-up-after-the-wordpress-widget-party/</guid>
		<description><![CDATA[In one of the recent posts &#8211; &#8220;Advanced Widgets. Widgets with controls.&#8221; &#8211; we saw how to create WordPress widgets, which could have configuration options.  In one of the comments to that post, Matthew Smith, asked a very good question:
do the widgets leave settings in the database upon removal? Should these be cleaned up using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=71&subd=wpbits&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In one of the recent posts &#8211; &#8220;<a href="http://wpbits.wordpress.com/2007/06/17/advanced-widgets-widgets-with-controls/" title="How to make WordPress widgets with options">Advanced Widgets. Widgets with controls.</a>&#8221; &#8211; we saw how to create WordPress widgets, which could have configuration options.  In <a href="http://wpbits.wordpress.com/2007/06/17/advanced-widgets-widgets-with-controls/#comment-348">one of the comments</a> to that post, <a href="http://digivation.net/">Matthew Smith</a>, asked a very good question:</p>
<blockquote><p>do the widgets leave settings in the database upon removal? Should these be cleaned up using a hook like <code>unregister_widget()</code> (if it exists, I haven’t looked yet)? Or does WordPress do this automatically?</p></blockquote>
<p>Nicely spotted, Matthew!  Thank you.</p>
<p>Indeed, what happens there?</p>
<p><span id="more-71"></span></p>
<p>Widget settings are stored in regular WordPress options.  Some widgets can be re-used several times (like Text widget and  RSS widget).  This can cause plenty of data to be flowing around.  That, in turn, can affect web site&#8217;s performance and server&#8217;s memory usage.</p>
<p>What can we do about it?   It&#8217;s obvious that we have to do some sort of clean-up routine, but how, when and where?</p>
<p>To answer those questions, let&#8217;s refresh our memory with how we got ourselves into that mess in the first place.  WordPress options are updated and populate from within our widget control.  Here is the control code that we used for that article:</p>
<p><img src="http://wpbits.files.wordpress.com/2007/06/advanced_date_widget_control.png" alt="Control for advanced date widget" /></p>
<p>Here we see the call to <em>update_option()</em>, which populates the database with our stuff.   This routine is registered as widget control, using the following line (again, the code is from the previous article):</p>
<p><img src="http://wpbits.files.wordpress.com/2007/06/register_sidebar_widget_with_control.png" alt="Register sidebar widget with control" /></p>
<p>Here is a call to <em>register_widget_control()</em> function.  This is how we add stuff.  So, to remove stuff, there should be something like <em>unregister_widget_control()</em> somewhere.  Let&#8217;s look through WordPress code for a bit&#8230; ah, here it is, found it!  <em>wp-includes/widgets.php</em> (no link to <a href="http://wordpress.taragana.net/">WordPress Source</a> this time, as it features version 2.1-alpha2, which didn&#8217;t have widgets yet, so use your own copy of WordPress for reference)has definitions of all widget things that we saw registering &#8211; <em>unregister_sidebar()</em>, <em>unregister_sidebar_widget()</em>, and <em>unregister_widget_control()</em> .</p>
<p>OK, now that we know that it would be nice to call <em>unregister_widget_control()</em> and <em>delete_option()</em> for our widget, where is the best place to do so?  I mean, if we just add those calls to our widget file, it will get unregistered and we won&#8217;t be able to use it.  How can we know when is our widget in use and when it&#8217;s not and when is it the best time to do the cleanup?</p>
<p>The answer to that, of course, depends a lot on how you do things.  There are many places to create widgets (themes, plugins), so there is no single recipe here.   But from the first glance, it seems, that putting all widget stuff into a  plugin makes sense.  When the plugin is activated, both the widget and its control are registered, and the database option is populated.  When the plugin is deactivated, the widget and the control are unregistered and the database option cleaned up.</p>
<p>We&#8217;ll look at how to create widget plugins in one of the upcoming posts.  (There are still a few issues in that area that I need to figure out before I can do a full blown post on that.)</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/wpbits.wordpress.com/71/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/wpbits.wordpress.com/71/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wpbits.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wpbits.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wpbits.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wpbits.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wpbits.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wpbits.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wpbits.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wpbits.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wpbits.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wpbits.wordpress.com/71/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=71&subd=wpbits&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://wpbits.wordpress.com/2007/08/11/cleaning-up-after-the-wordpress-widget-party/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/54bfadc28c22bf76402608db646cd031?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mamchenkov</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/advanced_date_widget_control.png" medium="image">
			<media:title type="html">Control for advanced date widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/register_sidebar_widget_with_control.png" medium="image">
			<media:title type="html">Register sidebar widget with control</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing WordPress plugin. Widget Loader.</title>
		<link>http://wpbits.wordpress.com/2007/06/18/writing-wordpress-plugin-widget-loader/</link>
		<comments>http://wpbits.wordpress.com/2007/06/18/writing-wordpress-plugin-widget-loader/#comments</comments>
		<pubDate>Mon, 18 Jun 2007 09:55:15 +0000</pubDate>
		<dc:creator>Leonid Mamchenkov</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Widgets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpbits.wordpress.com/2007/06/18/writing-wordpress-plugin-widget-loader/</guid>
		<description><![CDATA[One of the best things about WordPress is its flexibility.  For any given problem there are several solutions (&#8220;There is more than one way to do it&#8221; paradigm at work).  There are many places where you can add code and markup &#8211; widgets, templates, plugins&#8230;
In this post, we&#8217;ll look at WordPress plugins.  [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=14&subd=wpbits&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>One of the best things about WordPress is its flexibility.  For any given problem there are several solutions (&#8220;There is more than one way to do it&#8221; paradigm at work).  There are many places where you can add code and markup &#8211; widgets, templates, plugins&#8230;</p>
<p>In this post, we&#8217;ll look at WordPress plugins.  What are they?  What can we do with them?  How do we write them?  We&#8217;ll go as far as create our own WordPress plugin from scratch.  And it will be a useful one too.</p>
<p><span id="more-14"></span></p>
<p><a href="http://codex.wordpress.org/Writing_a_Plugin">WordPress plugins</a> are simply chunks of PHP code.  They are very useful for separating logical pieces of functionality of your web site for easier maintenance and reuse.  If you know how to program PHP, you can make your web site do anything you want it to.  If you don&#8217;t know how to program, you can browse through available plugins (see <a href="http://wp-plugins.net/" title="Excellent collection of WordPress plugins">WordPress plugin directory</a> for example) and choose pieces that suit your needs and wants &#8211; usually, installation and configuration of WordPress plugin requires very little technical expertise.</p>
<p>I like thinking about WordPress plugins as bricks.  Bricks aren&#8217;t very useful or interesting on their own.  Similarly, WordPress plugins aren&#8217;t of much interest when examined separately from WordPress.  But when bricks are put together, when they are mixed with some concrete and architect&#8217;s creativity, we can see them emerge into some amazing structures.  Similarly, WordPress plugins, when mixed with themes and widgets, can make a web site to really stand out.</p>
<p>Let&#8217;s jump into action now.  Let&#8217;s solve some boring problem with a WordPress plugin.  Let&#8217;s make something useful.  Do you have any ideas yet?</p>
<p>Well, I do.  During our widget tutorials, I kept doing the same thing over and over again &#8211; registering widgets.  For each widget that I want to use for my web site, I had to have this piece of code added to my theme&#8217;s functions.php:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/register_date_widget.png" alt="Register date widget" /></p>
<p>It&#8217;s a small chunk of code.  It&#8217;s easy to understand and all. But what happens if I have dozens of widgets that I want to use?  My <em>functions.php</em> grows and grows.  What happens when I want to use the same widget or two for another web site of mine?  I have to do plenty of scrolling, coping, and pasting.  This doesn&#8217;t sound like a lot of fun.  There should be a better way.</p>
<p>What if I could just have a separate file for each widget (which I already have anyway), and what if I could just copy this file somewhere into my theme directory and the widget contained in that file just be available for me in the administration area?  That would make my life so much easier!</p>
<p>Can I have it so?  The answer is &#8220;yes&#8221;.  &#8220;Yes, of course&#8221;, even.</p>
<p>I can have a plugin, which, when enabled, would look at a predefined place in my theme, find all widget files, and automatically register them.  Let&#8217;s starting writing it.</p>
<p>WordPress plugins live in <em>wp-content/plugins/</em> subdirectory.  They can be simple PHP files, or they can have directories and subdirectories, CSS styles, JavaScripts, and images of their own.  Like widgets, WordPress plugins can be as simple as a single command, or as complex as a standalone application.</p>
<p>For my plugin, which I decided to call <strong>Widget Loader</strong> (yes, very creative, I know), I&#8217;ll need a single file.  No need for a subdirectory or anything like that.  Here is how I start my plugin <em>widget_loader.php</em>:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/plugin_meta_comment.png" alt="Plugin comment with meta information" /></p>
<p>WordPress plugins start with a comment, which provides some information about the plugin, which WordPress displays in plugin administration interface.  Things like plugin name, version, information about author, license and stuff like that, all go into this comment. (See <a href="http://codex.wordpress.org/Plugin_API">WordPress Plugin API</a> for more information)</p>
<p>Now we can add PHP code for our plugin.  For Widget Loader, all we need is a simple function, which will look for files in <em>widget/</em> subdirectory of the current theme, and <em>include()</em> them.  Here is the code:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/load_widgets_function.png" alt="Load widgets function" /></p>
<p>Small and simple.  Just the way I like it (see <a href="http://en.wikipedia.org/wiki/KISS_principle">KISS principle</a>).  There are a few things to note though:</p>
<ul>
<li><strong>Function name</strong> &#8211; lm_widget_loader.  We want our function name to be as unique as possible, to avoid name clashes with functions from other plugins that we might use on our web site.  Here, I&#8217;m prepend my initials to the function name.</li>
<li><strong>Safety</strong>.  There is no such thing as too safe when it comes to WordPress plugins.  WordPress is a popular application, which runs on a number of platforms, with varying configurations.  This brings a lot of potential for things being not exactly as you&#8217;d expect them.  So, it&#8217;s a good idea to check your assumptions &#8211; WordPress versions, availability of functions that you rely on, PHP version, filesystem operations, database connectivity, etc.  Before using something, check that it&#8217;s there and that you have enough rights to use it.</li>
<li><strong>Cleanup</strong>.  With this simple code snippets it doesn&#8217;t look like a big problem if you leave something hanging &#8211; an open file or directory, an extra database connection, or a large data chunk in memory.  But.  You should always remember the fact that most WordPress web sites have multiple plugins enabled.  When loose ends hang from a dozen plugins, things get ugly.  Web sites slow down, error messages pile up, and things get hard to troubleshoot. So, cleanup after yourself.</li>
<li><strong>TEMPLATEPATH</strong>.  WordPress provides plenty of handy functions and variables to make plugin developers&#8217; lives easier.  Things can be done the old way (&#8220;I&#8217;ll write that thing myself.&#8221;) or they can be done the WordPress way (there is a function or variable set for everything).  It might take you some time and <a href="http://codex.wordpress.org" title="WordPress documentation">Codex</a> digging to get from one stage to another.</li>
</ul>
<p>Our plugin is almost done now.  In fact, it is a valid plugin, and if you save it in <em>wp-content/plugins/widget_loader.php</em> , you will immediately see it appear in plugin administration interface inside WordPress.  Activate it there, and you will be able to use <em>lm_load_widgets()</em> function call from anywhere in your theme or other plugins.</p>
<p>And that is the thing that I don&#8217;t really like now.  All my widgets are loading just fine, but I have to remember to call <em>lm_load_widgets()</em> from my theme.  And what if I give this plugin to somebody who is not very technical?  They&#8217;ll have to edit their theme files, which can lead to all sorts of problems.</p>
<p>Instead, I want this function to be called automatically.  I want WordPress to handle it on its own.  It should be smart enough.  And, in fact, it is.</p>
<p>WordPress has those things called &#8220;<a href="http:http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters//codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters">hooks</a>&#8220;.  Hooks are programming interfaces that allow us, programmers, change the way WordPress behaves, but from within our plugins and themes, without the need for editing of WordPress core files.  There are two types of hooks &#8211; <strong>actions</strong> and <strong>filters</strong>.  Filters allow us to change WordPress output, such as post title or user comment. (We&#8217;ll talk about these in another post later on).  Actions allow us to tell WordPress what we want it to do when it gets to a certain point, such as printing comment form, inserting post into the database, or checking user&#8217;s credentials.</p>
<p>For our little Widget Loader plugin, we want to tell WordPress to call <em>lm_widget_loader()</em> function whenever it initializes (basically, when it loads all that other stuff and prepares to serve a request).  There is a hook just for that.  It&#8217;s called &#8220;<a href="http://wphooks.flatearth.org/hooks/init/">init</a>&#8220;.  (You can see which other hooks are available together with some documentation for them at <a href="http://wphooks.flatearth.org/">WordPress Hooks</a> web site.)  Here is how we use it:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/add_action_call.png" alt="Function call to add action" /></p>
<p>That&#8217;s it. Just in case you missed or misunderstood something, here is the full text of our <em>wp-content/plugins/widget_loader.php</em> file:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/widget_loader_plugin.png" alt="Widget Loader plugin" /></p>
<p>Once this plugin is activated, we can just drop some widget files into <em>widget/</em> subdirectory of our theme.  To remove a widget, we just delete it from that subdirectory.  To edit the widget, we just edit the file.  No need to do tonnes of boring copy-paste.  Chances of breaking something are even smaller now.  And we can easily reuse bits and pieces between different themes and even between different web sites.</p>
<p>We&#8217;ll talk more about WordPress plugins in the upcoming posts&#8230;</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/wpbits.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/wpbits.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wpbits.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wpbits.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wpbits.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wpbits.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wpbits.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wpbits.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wpbits.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wpbits.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wpbits.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wpbits.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=14&subd=wpbits&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://wpbits.wordpress.com/2007/06/18/writing-wordpress-plugin-widget-loader/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/54bfadc28c22bf76402608db646cd031?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mamchenkov</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/register_date_widget.png" medium="image">
			<media:title type="html">Register date widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/plugin_meta_comment.png" medium="image">
			<media:title type="html">Plugin comment with meta information</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/load_widgets_function.png" medium="image">
			<media:title type="html">Load widgets function</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/add_action_call.png" medium="image">
			<media:title type="html">Function call to add action</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/widget_loader_plugin.png" medium="image">
			<media:title type="html">Widget Loader plugin</media:title>
		</media:content>
	</item>
		<item>
		<title>Advanced widgets. Widgets with controls.</title>
		<link>http://wpbits.wordpress.com/2007/06/17/advanced-widgets-widgets-with-controls/</link>
		<comments>http://wpbits.wordpress.com/2007/06/17/advanced-widgets-widgets-with-controls/#comments</comments>
		<pubDate>Sun, 17 Jun 2007 15:02:29 +0000</pubDate>
		<dc:creator>Leonid Mamchenkov</dc:creator>
				<category><![CDATA[Widgets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpbits.wordpress.com/2007/06/17/advanced-widgets-widgets-with-controls/</guid>
		<description><![CDATA[Hopefully, by now you understand how cool and powerful WordPress widgets are.  Widgets 101 demonstrated how to create simple widgets with text paragraphs, HTML markup, and even some PHP code.  What else is there?  Not much, but something.
In this post we&#8217;ll take a look at how to create widgets which support options [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=13&subd=wpbits&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Hopefully, by now you understand how cool and powerful WordPress widgets are.  <a href="http://wpbits.wordpress.com/2007/06/16/simple-widgets-widgets-101/">Widgets 101</a> demonstrated how to create simple widgets with text paragraphs, HTML markup, and even some PHP code.  What else is there?  Not much, but something.</p>
<p>In this post we&#8217;ll take a look at how to create widgets which support options and easy configuration via administration interface.  WordPress calls these widgets with controls.</p>
<p><span id="more-13"></span></p>
<p>Let&#8217;s reuse one of the examples from the <a href="http://wpbits.wordpress.com/2007/06/16/simple-widgets-widgets-101/">Simple Widgets</a> post &#8211; the Date widget.  Here is how it looks so far, without any controls.</p>
<p>We have all output in <em>widgets/current_date.php</em> file:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/date_widget.png" alt="Date widget" /></p>
<p>And we have registered this widget in <em>functions.php</em> file of our theme like so:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/register_date_widget.png" alt="Register date widget" /></p>
<p>It works and seem OK, doesn&#8217;t it?  Well, it does. But what if we want to change the format of the date?  What if instead of &#8220;February 23, 2007&#8243; we want to use &#8220;23 Feb 2007&#8243;? We&#8217;ll have to edit the<em> file widgets/current_date.php</em> and correct the <em>date()</em> format.  This doesn&#8217;t seem right.  WordPress is a beautiful, flexible platform, and we shouldn&#8217;t be editing PHP files for simple changes like that.</p>
<p>And, indeed, we don&#8217;t have to. Let&#8217;s upgrade this widget. We&#8217;ll give it a control &#8211; a configuration screen, where users will be able to edit <em>date()</em> formats.   For this to work, we&#8217;ll have to do the following:</p>
<ol>
<li>Upgrade our output function to understand options.</li>
<li>Create a new function which will manage option editing.</li>
<li>Register that new option editing function as widget control.</li>
</ol>
<p>Here is how the new function for date printing looks:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/advanced_date_widget.png" alt="Advanced date widget" /></p>
<p>All in all, it is very similar to what we had before.  The core functionality of this widget is still date printing.  Now, for our control function:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/advanced_date_widget_control.png" alt="Control for advanced date widget" /></p>
<p>Of course, you can have several options in there, text paragraphs with option descriptions, images with instructions, and so on.  Play with it.  Try new things out.  By the way, this code above was adopted from the WordPress&#8217; core code for default widgets.</p>
<p>We have all the pieces now.  It&#8217;s time to put them all together.  I mean, we should proceed to step 3 of the list above and register the control for the widget.  Here is how both widget and widget control are registered in my <em>functions.php</em> file:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/register_sidebar_widget_with_control.png" alt="Register sidebar widget with control" /></p>
<p>In the administration interface, where I drag-and-drop the widgets, I now have a box called &#8216;Date&#8217;.  When I drop it into one of my widget containers, I see that my &#8216;Date&#8217; box also has an icon.  When I click on the icon, a small window pops up, which asks me for <em>date()</em> format.</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/date_widget_control_popup.png" alt="Date widget control popup window" /></p>
<p>Now you know all about WordPress widgets that there is to know. Enjoy.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/wpbits.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/wpbits.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wpbits.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wpbits.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wpbits.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wpbits.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wpbits.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wpbits.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wpbits.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wpbits.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wpbits.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wpbits.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=13&subd=wpbits&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://wpbits.wordpress.com/2007/06/17/advanced-widgets-widgets-with-controls/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/54bfadc28c22bf76402608db646cd031?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mamchenkov</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/date_widget.png" medium="image">
			<media:title type="html">Date widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/register_date_widget.png" medium="image">
			<media:title type="html">Register date widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/advanced_date_widget.png" medium="image">
			<media:title type="html">Advanced date widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/advanced_date_widget_control.png" medium="image">
			<media:title type="html">Control for advanced date widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/register_sidebar_widget_with_control.png" medium="image">
			<media:title type="html">Register sidebar widget with control</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/date_widget_control_popup.png" medium="image">
			<media:title type="html">Date widget control popup window</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple widgets. Widgets 101.</title>
		<link>http://wpbits.wordpress.com/2007/06/16/simple-widgets-widgets-101/</link>
		<comments>http://wpbits.wordpress.com/2007/06/16/simple-widgets-widgets-101/#comments</comments>
		<pubDate>Sat, 16 Jun 2007 22:01:03 +0000</pubDate>
		<dc:creator>Leonid Mamchenkov</dc:creator>
				<category><![CDATA[Widgets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpbits.wordpress.com/2007/06/16/simple-widgets-widgets-101/</guid>
		<description><![CDATA[In the last post I covered widget containers and promised to tell you how to create widgets.  I also mentioned that widgets can be of a varying complexity &#8211; from simple text and HTML blocks, through customized WordPress function calls, to mini applications with their own administration interface.
That sounds like a lot to cover [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=7&subd=wpbits&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In the last post I covered <a href="http://wpbits.wordpress.com/2007/06/16/widgetized-web-site-beyond-sidebars/">widget containers</a> and promised to tell you how to create widgets.  I also mentioned that widgets can be of a varying complexity &#8211; from simple text and HTML blocks, through customized WordPress function calls, to mini applications with their own administration interface.</p>
<p>That sounds like a lot to cover in one post.  So, I&#8217;ll leave the advanced widgets for the next time.  In this post we&#8217;ll look at simple things &#8211; text, HTML, some PHP and tailored WordPress function calls.</p>
<p><span id="more-7"></span></p>
<p>Creating widgets is a simple two-step process.  Here is an overview:</p>
<ol>
<li>Create a function that prints something out.</li>
<li>Register function from step 1 as a widget.</li>
</ol>
<p>Let&#8217;s make the simplest of all examples &#8211; a &#8216;Hello World&#8217; widget.  For step 1, we create a function in our theme&#8217;s <em>functions.php</em> file (it can be anywhere in your theme or plugins actually, but we&#8217;ll try to maintain some order).</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/function_hello_world.png" alt="Hello, World!" /></p>
<p>Now, we should register our little function as a widget.  We do so by adding these lines to the <em>functions.php</em> file (again, you can choose another location):</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/register_sidebar_widget.png" alt="Register sidebar widget" /></p>
<p>That&#8217;s it.  All done.  Now you can login to the administration, navigate to <em>Presentation -&gt; Widgets</em>, and you should see a new &#8216;Hello World&#8217; widget available.  Drag-n-drop it to any of your widget containers (sidebars, remember?), save and reload the web page where the container is displayed.  You should see the &#8216;Hello, world!&#8217; string printed out.</p>
<p>That was easy, wasn&#8217;t it?</p>
<p>Before you blow up your <em>functions.php</em> file with gadzillion of widgets, let me share this one tip with you.  Create a subdirectory <em>widgets/</em> in your theme&#8217;s main directory, and keep your widgets in there. One per file.  Then, simply <em>include()</em> your widgets.  Here is an updated example with this tip implemented:</p>
<p>In <em>wp-content/themes/mytheme/widgets/hello_world.php</em>:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/div_hello_world.png" alt="DIV with Hello world" /></p>
<p>In <em>wp-content/themes/mytheme/functions.php</em>:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/hello_world_widget_with_include.png" alt="Hello world widget with include" /></p>
<p>Now you have it with HTML.  And you can use CSS to make it look good.But how about a little bit more functional example?  No problem.  Let&#8217;s go for something simple, but yet something practical, something that you could use for your web site (not necessarily a blog) &#8211; current date widget.</p>
<p>It seems that every web site out there considers it its own obligation to tell you what date it is, what day of week it is, and what time is it exactly in some time zone you&#8217;ve never heard of.  A perfect task for a widget.</p>
<p>Here is my <em>widgets/current_date.php</em> file (refresh your memory of PHP <a href="http://www.php.net/date">date()</a> function):</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/date_widget.png" alt="Date widget" /></p>
<p><code></code>Now I create a widget function and register it in my functions.php like so:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/register_date_widget.png" alt="Register date widget" /></p>
<p>Done.  All I have to do now is only drag-n-drop this widget to the widget container that I want.</p>
<p>Obviously, you can have as much code and markup as you need in your widget file.  You can use WordPress functions (such as <em>wp_get_links()</em> or <em>wp_get_pages()</em>), WordPress logic (such as <em>is_home()</em>, <em>is_page()</em>, and <em>is_search()</em>).  You can include more files.  You can do anything you like.  You don&#8217;t even necessarily have to output anything, but I think it&#8217;s still a good idea to.  Leave a trace on the page, even if that is just a space.  It&#8217;ll make troubleshooting much easier later on.</p>
<p>I&#8217;m sure you have plenty of ideas now about how to utilize this widget knowledge.  But if your mind is blank, here is a hint for you.  Every time you come across something that says &#8220;Copy this piece of code and paste it into your web site&#8217;s template&#8221;, make it into a widget.  Every time you see a banner or button that you want to include in your web site &#8211; make a widget.  Pretty soon, you&#8217;ll start thinking that everything can be a WordPress widget.  And that is so close to the truth that I won&#8217;t even argue with you.</p>
<p>So long, until next time, when we&#8217;ll look at advanced widgets &#8211; those that have options and need configuration.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/wpbits.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/wpbits.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wpbits.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wpbits.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wpbits.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wpbits.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wpbits.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wpbits.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wpbits.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wpbits.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wpbits.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wpbits.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=7&subd=wpbits&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://wpbits.wordpress.com/2007/06/16/simple-widgets-widgets-101/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/54bfadc28c22bf76402608db646cd031?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mamchenkov</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/function_hello_world.png" medium="image">
			<media:title type="html">Hello, World!</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/register_sidebar_widget.png" medium="image">
			<media:title type="html">Register sidebar widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/div_hello_world.png" medium="image">
			<media:title type="html">DIV with Hello world</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/hello_world_widget_with_include.png" medium="image">
			<media:title type="html">Hello world widget with include</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/date_widget.png" medium="image">
			<media:title type="html">Date widget</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/register_date_widget.png" medium="image">
			<media:title type="html">Register date widget</media:title>
		</media:content>
	</item>
		<item>
		<title>Widgetized web site. Beyond sidebars.</title>
		<link>http://wpbits.wordpress.com/2007/06/16/widgetized-web-site-beyond-sidebars/</link>
		<comments>http://wpbits.wordpress.com/2007/06/16/widgetized-web-site-beyond-sidebars/#comments</comments>
		<pubDate>Sat, 16 Jun 2007 11:23:34 +0000</pubDate>
		<dc:creator>Leonid Mamchenkov</dc:creator>
				<category><![CDATA[Widgets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpbits.wordpress.com/2007/06/16/widgetized-web-site-beyond-sidebars/</guid>
		<description><![CDATA[WordPress widget is a powerful tool when used properly.  Too bad, many people who use WordPress for web site building haven&#8217;t heard of them.  And many of those who heard of them, don&#8217;t realize the full potential.  In this post, I&#8217;ll try to put some light on WordPress widgets.
So, what is a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=6&subd=wpbits&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>WordPress widget is a powerful tool when used properly.  Too bad, many people who use WordPress for web site building haven&#8217;t heard of them.  And many of those who heard of them, don&#8217;t realize the full potential.  In this post, I&#8217;ll try to put some light on WordPress widgets.</p>
<p>So, what is a widget and what is it good for?  WordPress widget is a block of something.  It can be as simple as a paragraph of text or a piece of HTML markup.  It can be a customized call to some WordPress function.  It can be a mini application.  It can have its own options. And it can have its own administration area.</p>
<p><span id="more-6"></span></p>
<p>WordPress comes with a few default widgets, which are very useful for blog web sites.  These are things like search boxes, archive lists, links lists, recent posts and comments, etc.</p>
<p>WordPress provides a way to create a sort of container, where you can put your widgets.  In most cases that I&#8217;ve seen, this container is placed in sidebar &#8211; a place where such functionality belongs on a blog.</p>
<p>But the thing is that WordPress puts no limitation on where you can use widgets.  You can  create a container on your article page, or in the search results, or on a front page.  It doesn&#8217;t matter.  And what is even better, you can have several such containers, each with unique set of widgets.</p>
<p>So, why would one want to use widgets instead of just coding these blocks in the theme?  Because, widgets can be easily sorted, added to the site, removed from the site, or moved from one place to another &#8211; all that without any need for coding.  Administration interface has a really simple drag-and-drop interface.  Define widget containers in your theme, code some widgets, and then drag them and drop them anyway you like. It&#8217;s that easy.</p>
<p>Can we see an example?  Sure, why not.</p>
<p>First of all, you need to register your widget container, which is confusingly called sidebar.  Here is how to do it.  In one of your theme files (I prefer functions.php, but you can use anything at all &#8211; index.php, sidebar.php, search.php, whatever), add these lines:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/register_sidebar.png" alt="Register sidebar" /></p>
<p>You just created a widget container called &#8216;Widget Home&#8217;. (Here is a good description of <a href="http://automattic.com/code/widgets/api/">widget API</a>, if you need one.)  Now we need to place these container somewhere in the theme.  Somewhere, where we want it to appear.  This is up to you.  You can choose any place at all &#8211; front page, search results, sidebar, footer, you name it.  Once you decided where you want it to be, add these lines of code:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/dynamic_sidebar.png" alt="Dynamic sidebar" /></p>
<p>If you will accidentally use your theme on a WordPress installation which does not support widgets, or if your widget container is empty, you&#8217;ll have a &#8220;Widget Home is broken.&#8221; string printed out.  You can, of course, change it.  For example, you can have a fallback include file, which you can call with <em>include(&#8216;missing_widgets.php&#8217;)</em>.</p>
<p>So far so good.  What&#8217;s next?</p>
<p>Now you need to place some widgets into your widget container.  Login to administration interface and navigate to Presentation -&gt; Widgets.  You&#8217;ll see a rectangle with &#8216;Widget Home&#8217; title on top.  You&#8217;ll see a rectangle with &#8216;Available Widgets&#8217; below.  And you&#8217;ll see some widget boxes in there, such as &#8216;Recent comments&#8217; and &#8216;Links&#8217;.  Simply drag-and-drop these widget boxes into your widget container, reoder them as you see fit, and save your layout.  Check your web site. Cool, isn&#8217;t it?</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/widgets_home_sidebar.png" alt="Custom sidebar" /></p>
<p>See, widgets are not only for sidebars.  They are for anything and everything.  You can have several widget containers (sidebars, eh?) defined.  For example,  you can have a setup like this:</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/multiple_sidebars.png" alt="Multiple sidebars" /></p>
<p>In your administration area, you&#8217;ll see three widget containers now &#8211; &#8216;Left sidebar&#8217;, &#8216;Main area&#8217;, and &#8216;Right sidebar&#8217;.  You can drag-and-drop widgets between them.  And you can use them from in your theme like so (in index.php file, for example):</p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/template_with_multiple_sidebars.png" alt="Template with multiple sidebars" /></p>
<p>Now your front page can be easily customized and organized with some drag-and-drop in the administration area.  No need to play with PHP files anymore.</p>
<p>In the next post, we&#8217;ll see how to create our own widgets.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/wpbits.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/wpbits.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wpbits.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wpbits.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wpbits.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wpbits.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wpbits.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wpbits.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wpbits.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wpbits.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wpbits.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wpbits.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=6&subd=wpbits&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://wpbits.wordpress.com/2007/06/16/widgetized-web-site-beyond-sidebars/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/54bfadc28c22bf76402608db646cd031?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mamchenkov</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/register_sidebar.png" medium="image">
			<media:title type="html">Register sidebar</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/dynamic_sidebar.png" medium="image">
			<media:title type="html">Dynamic sidebar</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/widgets_home_sidebar.png" medium="image">
			<media:title type="html">Custom sidebar</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/multiple_sidebars.png" medium="image">
			<media:title type="html">Multiple sidebars</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/template_with_multiple_sidebars.png" medium="image">
			<media:title type="html">Template with multiple sidebars</media:title>
		</media:content>
	</item>
		<item>
		<title>Disable default WordPress widgets</title>
		<link>http://wpbits.wordpress.com/2007/06/15/disable-default-wordpress-widgets/</link>
		<comments>http://wpbits.wordpress.com/2007/06/15/disable-default-wordpress-widgets/#comments</comments>
		<pubDate>Fri, 15 Jun 2007 20:45:47 +0000</pubDate>
		<dc:creator>Leonid Mamchenkov</dc:creator>
				<category><![CDATA[Widgets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpbits.wordpress.com/2007/06/15/disable-default-wordpress-widgets/</guid>
		<description><![CDATA[I am currently building a web site, which uses WordPress as a platform.  It&#8217;s not a blog, but an extended a very customized content management system (CMS) with several authors and a lot of custom plugins.
One of the things the client requested was maximum flexibility of the front page.  They want to have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=3&subd=wpbits&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I am currently building a web site, which uses WordPress as a platform.  It&#8217;s not a blog, but an extended a very customized content management system (CMS) with several authors and a lot of custom plugins.</p>
<p>One of the things the client requested was maximum flexibility of the front page.  They want to have a number of information blocks, such as currency quotes, market analysis, navigation menu, and advertising units; and they want to be able to rearrange them once in a while.</p>
<p>This is, of course, a perfect task for <a href="http://automattic.com/code/widgets/" title="WordPress widgets documentation">WordPress widgets</a>. If you are not familiar with WordPress widgets, I only have two things to say to you right now:</p>
<ol>
<li> You are missing on a lot of fun.</li>
<li>Subscribe to <a href="http://wpbits.wordpress.com/feed/">RSS feed</a> of this blog, as I&#8217;ll be talking a lot about WordPress widgets.</li>
</ol>
<p>Anyway. I have created all the widgets that they wanted, and I tested them to work properly.  But there was a tiny problem.  WordPress install comes with a few widgets of its own.  Things like archives, blogroll, recent comments, and search box &#8211; all have a widget version.  But most of these widgets, as good as they are for a regular blog, didn&#8217;t make any sense for this web site that I am working on.  There are no archives.  Calendar has a totally different meaning. And comments are disabled and hidden.</p>
<p>Of course, I could just left the widgets out of the sidebars.  But why give user a choice which doesn&#8217;t work or makes no sense, right?  I wanted them removed.  Hidden.  Unavailable.  And I didn&#8217;t want to modify any core WordPress files, so that upgrading this web site to a newer version of WordPress is easier in the future.</p>
<p>Here is my solution to the problem.</p>
<p><span id="more-3"></span></p>
<p>In the theme directory, I have a <em>functions.php</em> file (this file has <a href="http://codex.wordpress.org/Theme_Development" title="WordPress Theme development">special meaning</a> to WordPress).  In this file, I have these lines<code>:</code></p>
<p><a href="http://wpbits.files.wordpress.com/2007/06/hide_default_widgets.png" title="Hide default widgets"></a></p>
<p style="text-align:center;"><img src="http://wpbits.files.wordpress.com/2007/06/hide_default_widgets.png" alt="Hide default widgets" /></p>
<p>Here is what happens.  I define a function <em>remove_default_widgets()</em>, which is called for action <em>widgets_init</em> (more on this later, but if you are in a hurry, read file <em>wp-includes/widgets.php</em> around lines 922-958 in WordPress 2.2).  This action is called just after the default widgets are registered. In this function I simply unregister all default widgets.</p>
<p>Hopefully, you can spot two exceptions here.  Two special widgets are Text widget and RSS widget.  These two widgets have additional controls on the administration page.  They ask user for options (specifically: how many copies of each of these widgets user wants).  Since I don&#8217;t want to use these widgets, I don&#8217;t need their options too.  The options are initialized when <em>sidebar_admin_setup</em> and <em>sidebar_admin_page</em> actions take place.  By looking through <a href="http://wordpress.taragana.net/nav.html?index.html" title="WordPress Source Cross Reference">WordPress source code</a>, I manage to find the appropriate callbacks, which I can now easily cancel.</p>
<p>Done.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/wpbits.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/wpbits.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wpbits.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wpbits.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wpbits.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wpbits.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wpbits.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wpbits.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wpbits.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wpbits.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wpbits.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wpbits.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wpbits.wordpress.com&blog=1243059&post=3&subd=wpbits&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://wpbits.wordpress.com/2007/06/15/disable-default-wordpress-widgets/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/54bfadc28c22bf76402608db646cd031?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mamchenkov</media:title>
		</media:content>

		<media:content url="http://wpbits.files.wordpress.com/2007/06/hide_default_widgets.png" medium="image">
			<media:title type="html">Hide default widgets</media:title>
		</media:content>
	</item>
	</channel>
</rss>