<?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>WP Engineer &#187; Widget</title>
	<atom:link href="http://wpengineer.com/tag/widget/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpengineer.com</link>
	<description>WordPress News, Hacks, Tips, Tutorials, Plugins and Themes</description>
	<lastBuildDate>Mon, 21 May 2012 22:48:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WordPress Child Themes – Remove Widget Areas</title>
		<link>http://wpengineer.com/2049/wordpress-child-themes-remove-widget-areas/</link>
		<comments>http://wpengineer.com/2049/wordpress-child-themes-remove-widget-areas/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 10:00:08 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2049</guid>
		<description><![CDATA[In a WordPress Framework or Basic-Theme are one or several Widget Areas (Sidebars) defined. But how can you remove the widget areas if you don't need them and just confuse other backend users. The line unregister_sidebar('my-sidebar'); in functions.php of Child-Themes doesn't do anything. Why? WordPress loads first the function.php of Child-Themes and then of the [...]]]></description>
			<content:encoded><![CDATA[<p>In a WordPress Framework or Basic-Theme are one or several Widget Areas (Sidebars) defined. But how can you remove the widget areas if you don't need them and just confuse other backend users.<br />
<span id="more-2049"></span><br />
The line <code>unregister_sidebar('my-sidebar');</code> in functions.php of Child-Themes doesn't do anything. Why? WordPress loads first the <code>function.php</code> of Child-Themes and then of the Framework. The Sidebar, which we like to remove is not there yet. It will be registered while loading the Basic-Themes/Frameworks.</p>
<p>But you can solve the problem with a Hook in the registry of your Framework/Basic-Theme.</p>
<pre lang="php">
//Code of the Framework to register 2 Sidebars
function xtreme_register_dynamic_sidebars() {
  register_sidebar( array(
    &#039;name&#039; =&gt; &#039;Sidebar One&#039;,
    &#039;id&#039; =&gt; &#039;sidebar-one&#039;,
    &#039;description&#039; =&gt; &#039;Sidebar One&#039;,
    &#039;before_widget&#039; =&gt; &#039;&lt;li id=&quot;%1$s&quot; class=&quot;widget %2$s&quot;&gt;&#039;,
    &#039;after_widget&#039; =&gt; &#039;&lt;/li&gt;&#039;,
    &#039;before_title&#039; =&gt; &#039;&lt;h5 class=&quot;widget-title&quot;&gt;&#039;,
    &#039;after_title&#039; =&gt; &#039;&lt;/h5&gt;&#039;
    ));
  register_sidebar( array(
    &#039;name&#039; =&gt; &#039;Sidebar Two&#039;,
    &#039;id&#039; =&gt; &#039;sidebar-two&#039;,
    &#039;description&#039; =&gt; &#039;Sidebar Two&#039;,
    &#039;before_widget&#039; =&gt; &#039;  &lt;li id=&quot;%1$s&quot; class=&quot;widget %2$s&quot;&gt;&#039;,
    &#039;after_widget&#039; =&gt; &#039;&lt;/li&gt;&#039;,
    &#039;before_title&#039; =&gt; &#039;&lt;h5 class=&quot;widget-title&quot;&gt;&#039;,
    &#039;after_title&#039; =&gt; &#039;&lt;/h5&gt;&#039;
    ));
  do_action(&#039;childtheme_sidebars&#039;);
}
add_action( &#039;widgets_init&#039;, &#039;xtreme_register_dynamic_sidebars&#039; );
</pre>
<p>You see the Hook <code>do_action('childtheme_sidebars');</code><br />
let's use this Hook:</p>
<pre lang="php">
//functions.php im Child-Theme
function xtreme_unregister_sidebar() {
    unregister_sidebar(&#039;sidebar-two&#039;);
}
add_action( &#039;childtheme_sidebars&#039;, &#039;xtreme_unregister_sidebar&#039; );
</pre>
<p>And the Sidebar is removed. Easy huh? <img src='http://wpengineer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2049/wordpress-child-themes-remove-widget-areas/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>24th Door &#8211; The WPE Quit Smoking Widget</title>
		<link>http://wpengineer.com/1918/24th-door-the-wpe-quit-smoking-widget/</link>
		<comments>http://wpengineer.com/1918/24th-door-the-wpe-quit-smoking-widget/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 07:12:19 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[Widget API]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1918</guid>
		<description><![CDATA[For the last door in our Advent Calendar I had something special in mind. This year is coming to an end and I'm sure some of our readers want to quit smoking next year. To support their goal, I created a WP Engineer Quit Smoking Widget. Here a screenshot of all setting possibilities: And this [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://wpengineer.com/wp-content/uploads/WordPress-Christmas-24.jpg" alt="" title="WordPress-Christmas-24" width="600" height="400" class="aligncenter size-full wp-image-1902" />For the last door in our Advent Calendar I had something special in mind. This year is coming to an end and I'm sure some of our readers want to quit smoking next year. To support their goal, I created a WP Engineer Quit Smoking Widget. <img src='http://wpengineer.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><span id="more-1918"></span><br />
Here a screenshot of all setting possibilities:</p>
<p><img src="http://wpengineer.com/wp-content/uploads/quit-smoking-widget.png" alt="WP Engineer Quit Smoking Widget" title="WPengineer Quit Smoking Widget" width="510" height="432" class="aligncenter size-full wp-image-1919" /></p>
<p>And this is how it would look like on your blog:</p>
<p><img src="http://wpengineer.com/wp-content/uploads/widget-output.png" alt="WP Enigineer Quit Smoking Widget Output" title="WP Enigineer Quit Smoking Widget Output" width="290" height="97" class="aligncenter size-full wp-image-1920" /></p>
<p>Even though, this idea seemed to be easy to realize. It was a piece of work. I don't want you to be bored, so I'm not explaining the whole code, but I just want go in detail on some details, what I had to consider to make this Widget workable.</p>
<p>I check the date if it's valid ( 2009/02/30 doesn't exist). Then I had to avoid division by zero, if the Widget is active but doesn't include any values yet. For the correct formatting of the saved money depending on the language I used the WordPress function <strong>number_format_i18n</strong>:</p>
<pre lang="php">
$saved = number_format_i18n($packs * $price, 2);
</pre>
<p>Unfortunately, there is no feature in WordPress, which displays the currencies correctly (but I guess, this is probably not necessary). Therefore, I added a field "Currency position" with the values "before" and "after", so that the currency will be displayed properly, depending on the country. The year field automatically displays the current year minus 5 years, so that the widget is also current in 100 years. <img src='http://wpengineer.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
The rest are fields for entering the text output before and after values.</p>
<h3>Installation</h3>
<p>Unzip file wpe_quit_smoking_widget.zip and copy it to wp-content/plugins/ , then activate it in your backend. After that the Widget will be available in your Widgets area.</p>
<p>Please Note: The Widget needs <strong>WordPress Version 2.8</strong> or higher, otherwise it won't be displayed.</p>
<h3>Download</h3>
<p>Download <a href="http://wpengineer.com/?download=WP%20Engineer%20Quit%20Smoking%20Widget">WPEngineer Quit Smoking Widget</a></p>
<p>Have fun with the Widget and good luck with your goal to stop smoking! The team of WP Engineer wish you all Happy Holidays!</p>
<p><img src="http://wpengineer.com/wp-content/uploads/merry-christmas-wpengineer.jpg" alt="" title="merry-christmas-wpengineer" width="368" height="280" class="aligncenter size-full wp-image-1935" /><br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1918/24th-door-the-wpe-quit-smoking-widget/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Check for Widgets in Widget-Areas</title>
		<link>http://wpengineer.com/1873/check-for-widgets-in-widget-areas/</link>
		<comments>http://wpengineer.com/1873/check-for-widgets-in-widget-areas/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 07:28:11 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1873</guid>
		<description><![CDATA[Welcome to our 6th door of our Advent Calendar. If you use widgets in your theme and this also accommodates a variety of hierarchies, you will quickly realize that it really only makes sense to output HTML for the widgets when the widget area is also used by the user. Ian Stewart, the developer of [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://wpengineer.com/wp-content/uploads/WordPress-Christmas-06.jpg" alt="WordPress-Christmas-06" title="WordPress-Christmas-06" width="600" height="400" class="aligncenter size-full wp-image-1884" />Welcome to our 6th door of our Advent Calendar. <img src='http://wpengineer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you use widgets in your theme and this also accommodates a variety of hierarchies, you will quickly realize that it really only makes sense to output HTML for the widgets when the widget area is also used by the user.</p>
<p><span id="more-1873"></span><br />
<a href="http://themeshaper.com/">Ian Stewart</a>, the developer of the Theme-Framework <a href="http://code.google.com/p/thematic/">Thematic</a> found an elegant solution, but you can use it even without the framework.<br />
You just need one function, which is similar to the <a href="http://codex.wordpress.org/Conditional_Tags">Conditional Tags</a> of WordPress. Put this function in your <code>functions.php</code> of your theme.</p>
<pre lang="php">
function is_sidebar_active($index) {
	global $wp_registered_sidebars;

	$widgetcolums = wp_get_sidebars_widgets();

	if ($widgetcolums&#091;$index&#093;)
		return true;

	return false;
}
</pre>
<p>It uses the output of your widget area, if there is content.</p>
<p>A query could look like this in your template:</p>
<pre lang="php">
&lt;?php if ( function_exists(&#039;is_sidebar_active&#039;) &amp;&amp; is_sidebar_active(&#039;sidebar&#039;) ) { ?&gt;
	&lt;div id=&quot;sidebar&quot;&gt;
		&lt;ul&gt;
			&lt;?php dynamic_sidebar(&#039;sidebar&#039;); ?&gt;
		&lt;/ul&gt;
	&lt;/div&gt;
&lt;?php } ?&gt;
</pre>
<p>The above example loads the content only, if the widget area with the ID <code>sidebar</code> has content, that means if widgets are used in this area.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1873/check-for-widgets-in-widget-areas/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Advent Calendar &#8211; 24 Days Tips And Tricks Each Day! Today: WordPress Comment Form Widget</title>
		<link>http://wpengineer.com/1875/advent-calendar-24-days-tips-and-tricks-each-day-today-wordpress-comment-form-widget/</link>
		<comments>http://wpengineer.com/1875/advent-calendar-24-days-tips-and-tricks-each-day-today-wordpress-comment-form-widget/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 04:43:41 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[Widget API]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Themes]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1875</guid>
		<description><![CDATA[Especially in Germany we have the tradition of an Advent Calendar. An Advent calendar is a special calendar which is used to count down the days from December the 1st to Christmas Eve December 24th. An Advent Calendar has 24 doors, one of which is opened every day during Advent. Each door opens to reveal [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://wpengineer.com/wp-content/uploads/WordPress-Christmas-01.jpg" alt="WordPress-Christmas-01" title="WordPress-Christmas-01" width="600" height="400" class="aligncenter size-full wp-image-1879" /></p>
<p>Especially in Germany we have the tradition of an Advent Calendar. An Advent calendar is a special calendar which is used to count down the days from December the 1st to Christmas Eve December 24th. </p>
<p>An Advent Calendar has 24 doors, one of which is opened every day during Advent. Each door opens to reveal an image, a poem, or part of a story such as the Nativity story itself. More elaborate Advent calendars have a small gift concealed in each door, such as a toy or a chocolate item.</p>
<p>At WP Engineer, we thought we should do the same and open a door every day, which contains a little gift for our readers, by providing them a WordPress tip or trick each day. So we hope you will be exciting to see what tip, hack or news will be behind the door the next morning. Enjoy a new little WordPress gift every day until December 24th. <img src='http://wpengineer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Our first little gift is a code snippet to implement the commentform as a widget in your sidebar. This Widget only works with WordPress 2.8 and higher since it's using the new <a href="http://wpengineer.com/wordpress-built-a-widget/" title="WordPress Widget API">Widget API</a>. Just paste the code in the functions.php of your theme and drag the Widget on your sidebar.<br />
<span id="more-1875"></span></p>
<pre lang="php">
&lt;?php
class wpe_comment_widget extends WP_Widget {

    function wpe_comment_widget() {
        $widget_ops = array(&#039;classname&#039; =&gt; &#039;wpe_widget_comments&#039;, &#039;description&#039; =&gt; __( &#039;Your comments and commentform&#039;, &#039;your_textdomain&#039;) );
        $this-&gt;WP_Widget(&#039;wpe-comment-widget&#039;, __(&#039;WPE Comments&#039;, &#039;your_textdomain&#039;), $widget_ops);
    }

    function widget($args, $instance) {
        if(is_single()) {
            extract($args);
            global $post;
            echo $before_widget;
            comments_template();
            echo $after_widget;
        }
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        return $instance;
    }

    function form($instance) {
        ?&gt;
        &lt;p&gt;&lt;?php _e(&#039;This widget will only be displayed in single view.&#039;, &#039;your_textdomain&#039;); ?&gt;&lt;/p&gt;
       &lt;?php
    }
}

register_widget(&#039;wpe_comment_widget&#039;);
?&gt;
</pre>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1875/advent-calendar-24-days-tips-and-tricks-each-day-today-wordpress-comment-form-widget/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Deactivate WordPress Default Widgets</title>
		<link>http://wpengineer.com/1650/deactivate-wordpress-default-widgets/</link>
		<comments>http://wpengineer.com/1650/deactivate-wordpress-default-widgets/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:54:30 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Themes]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1650</guid>
		<description><![CDATA[You do not always want to have all the widgets active, which comes within the WordPress core. You can disable the unneccessary Widgets in your functions.php of your theme with a small function. The following syntax will switch off all the standard widgets. It should therefore be adjusted depending on your requirements: // unregister all [...]]]></description>
			<content:encoded><![CDATA[<p>You do not always want to have all the widgets active, which comes within the WordPress core.</p>
<p>You can disable the unneccessary Widgets in your <code>functions.php</code> of your theme with a small function. The following syntax will switch off all the standard widgets. It should therefore be adjusted depending on your requirements:</p>
<pre lang="php" >
// unregister all default WP Widgets
function unregister_default_wp_widgets() {

	unregister_widget(&#039;WP_Widget_Pages&#039;);

	unregister_widget(&#039;WP_Widget_Calendar&#039;);

	unregister_widget(&#039;WP_Widget_Archives&#039;);

	unregister_widget(&#039;WP_Widget_Links&#039;);

	unregister_widget(&#039;WP_Widget_Meta&#039;);

	unregister_widget(&#039;WP_Widget_Search&#039;);

	unregister_widget(&#039;WP_Widget_Text&#039;);

	unregister_widget(&#039;WP_Widget_Categories&#039;);

	unregister_widget(&#039;WP_Widget_Recent_Posts&#039;);

	unregister_widget(&#039;WP_Widget_Recent_Comments&#039;);

	unregister_widget(&#039;WP_Widget_RSS&#039;);

	unregister_widget(&#039;WP_Widget_Tag_Cloud&#039;);
}

add_action(&#039;widgets_init&#039;, &#039;unregister_default_wp_widgets&#039;, 1);
</pre>
<p>The function <code>unregister_widget()</code> needs as parameters the class, therefore the code is working since version 2.8 only. In prior versions, Widgets were supported differently.</p>
<p>How to create your own Widgets since WordPress Version 2.8 is explained in <a href="http://wpengineer.com/wordpress-built-a-widget/">Build A WordPress 2.8 Widget With The New Widget API</a>.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1650/deactivate-wordpress-default-widgets/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>WordPress 2.8 Single Post Navigation Widget</title>
		<link>http://wpengineer.com/1551/wordpress-2-8-single-post-navigation-widget/</link>
		<comments>http://wpengineer.com/1551/wordpress-2-8-single-post-navigation-widget/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 13:38:57 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[Widget API]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp2.8]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1551</guid>
		<description><![CDATA[Since WordPress 2.8, there is a new Widget API. In our post Build A WordPress 2.8 Widget With The New Widget API, I have used a simple example to describe how to build a Widget. This time Heiko and I've created something more complex. It is a Post Navigation Widget, which lists in the single [...]]]></description>
			<content:encoded><![CDATA[<p>Since WordPress 2.8, there is a new <strong>Widget API</strong>. In our post <a title="WordPress 2.8 Widget API" href="http://wpengineer.com/wordpress-built-a-widget/">Build A WordPress 2.8 Widget With The New Widget API</a>, I have used a simple example to describe how to build a <strong>Widget</strong>. This time Heiko and I've created something more complex.</p>
<p><span id="more-1551"></span><br />
It is a Post Navigation Widget, which lists in the single post view (single.php) a specific number of posts which were published before this post and a certain number of posts which were published after this post. I think this is a nice way to show older posts in the sidebar. Here's a screenshot, left of the backend, right of the sidebar:</p>
<p><img src="http://wpengineer.com/wp-content/uploads/post-navigation-widget.png" alt="WordPress Single Post Navigation Widget" title="WordPress Single Post Navigation Widget" width="505" height="386" class="aligncenter size-full wp-image-1553" /></p>
<p>I created a query to check if the class <strong>WP_Widget</strong> even exists, so the user won't get any error messages in WordPress versions prior to 2.8.</p>
<pre lang="php">
&lt;?php
if (class_exists(&#039;WP_Widget&#039;)) {
    class WPE_Widget_Post_Navigation extends WP_Widget {

        function WPE_Widget_Post_Navigation() {
            $widget_ops = array(&#039;classname&#039; =&gt; &#039;wpe_widget_post_navi&#039;, &#039;description&#039; =&gt; __( &quot;Some posts before and after the current post&quot;) );
            $this-&gt;WP_Widget(&#039;wpe-post-navi&#039;, __(&#039;WPE Single Post Navigation&#039;), $widget_ops);
        }

        function widget($args, $instance) {
            if(is_single()) {
                global $post, $wpdb;
                extract($args);

                if ( !$number = (int) $instance&#091;&#039;number&#039;&#093; )
                    $number = 5;
                elseif( $number &lt; 1 )
                    $number = 1;
                elseif( $number &gt; 10 )
                    $number = 10;

                $title_before 		= apply_filters(&#039;widget_title&#039;, empty($instance&#091;&#039;title_before&#039;&#093;) ? &#039;Posts before&#039; : $instance&#091;&#039;title_before&#039;&#093;);
                $title_after	 	= apply_filters(&#039;widget_title&#039;, empty($instance&#091;&#039;title_after&#039;&#093;) ? &#039;Posts after&#039; : $instance&#091;&#039;title_after&#039;&#093;);
                $before_widget_2	= preg_replace(&quot;/(wpe\-post\-navi-\d+)/&quot;, &quot;$1-1&quot;, $before_widget);

                $querystr = &quot;
                    SELECT *
                    FROM $wpdb-&gt;posts wposts
                    WHERE wposts.ID != $post-&gt;ID
                    AND wposts.post_type = &#039;post&#039;
                    AND wposts.post_status = &#039;publish&#039;
                    AND wposts.post_date %s &#039;$post-&gt;post_date&#039;
                    ORDER BY wposts.post_date %s
                    LIMIT $number
                 &quot;;

                $leading_posts = $wpdb-&gt;get_results(sprintf($querystr, &#039;&lt;&#039;, &#039;DESC&#039;), OBJECT);
                $trailing_posts = $wpdb-&gt;get_results(sprintf($querystr, &#039;&gt;&#039;, &#039;ASC&#039;), OBJECT);

                if ($trailing_posts &amp;&amp; count($trailing_posts)) {
                    echo $before_widget_2 . $before_title . $title_before . $after_title . &quot;&lt;ul&gt;&quot;;
                    $trailing_posts = array_reverse($trailing_posts);
                    foreach ($trailing_posts as $post) {
                        setup_postdata($post);
                        ?&gt; &lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; title=&quot;&lt;?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?&gt;&quot;&gt;&lt;?php if ( get_the_title() ) the_title(); else the_ID(); ?&gt; &lt;/a&gt;&lt;/li&gt;&lt;?php
                    }
                    echo &quot;&lt;/ul&gt;&quot; . $after_widget;
                }

                if ($leading_posts &amp;&amp; count($leading_posts)) {
                    echo $before_widget . $before_title . $title_after . $after_title . &quot;&lt;ul&gt;&quot;;
                    foreach ($leading_posts as $post) {
                        setup_postdata($post);
                        ?&gt; &lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; title=&quot;&lt;?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?&gt;&quot;&gt;&lt;?php if ( get_the_title() ) the_title(); else the_ID(); ?&gt; &lt;/a&gt;&lt;/li&gt;&lt;?php
                    }
                    echo &quot;&lt;/ul&gt;&quot; . $after_widget;
                }
                wp_reset_query();
            }
        }

        function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance&#091;&#039;title_before&#039;&#093; = strip_tags($new_instance&#091;&#039;title_before&#039;&#093;);
            $instance&#091;&#039;number&#039;&#093; = (int) $new_instance&#091;&#039;number&#039;&#093;;
            $instance&#091;&#039;title_after&#039;&#093; = strip_tags($new_instance&#091;&#039;title_after&#039;&#093;);

            return $instance;
        }
        function form( $instance ) {
            $title_before = ($instance&#091;&#039;title_before&#039;&#093; == &#039;&#039;) ? &#039;Posts before&#039; : esc_attr($instance&#091;&#039;title_before&#039;&#093;);
            $title_after = ($instance&#091;&#039;title_after&#039;&#093; == &#039;&#039;) ? &#039;Posts after&#039; : esc_attr($instance&#091;&#039;title_after&#039;&#093;);
            if ( !$number = (int) $instance&#091;&#039;number&#039;&#093; )
                $number = 5;
            elseif ( $number &lt; 1 )
                $number = 1;
            elseif ( $number &gt; 10 )
                $number = 10;
    ?&gt;
            &lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;title_before&#039;); ?&gt;&quot;&gt;Title before:&lt;/label&gt;
            &lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;title_before&#039;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#039;title_before&#039;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo $title_before; ?&gt;&quot; /&gt;&lt;/p&gt;
            &lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;title_after&#039;); ?&gt;&quot;&gt;Title after:&lt;/label&gt;
            &lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;title_after&#039;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#039;title_after&#039;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo $title_after; ?&gt;&quot; /&gt;&lt;/p&gt;
            &lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;number&#039;); ?&gt;&quot;&gt;Number of posts to show before and after current post:&lt;/label&gt;
            &lt;input id=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;number&#039;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#039;number&#039;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo $number; ?&gt;&quot; size=&quot;3&quot; /&gt;&lt;br /&gt;
            &lt;small&gt;(at most 10)&lt;/small&gt;&lt;/p&gt;
    &lt;?php
        }
    }
    register_widget(&#039;WPE_Widget_Post_Navigation&#039;);
}
?&gt;
</pre>
<p>Most of this should be self-explanatory. Now to the function <strong>widget () </strong>, which does the actual work. Here we had to do a little trick, because the widget actually creates 2 widgets, and they would get the same ID. That wouldn't be valid, so the code adds a -1 to one of the widget elements:</p>
<pre lang="php">
$before_widget_2 = preg_replace(&quot;/(wpe\-post\-navi-\d+)/&quot;, &quot;$1-1&quot;, $before_widget);
</pre>
<p>2 queries are getting executed. One for the posts before this post and one for the posts after this post. if posts are available they will show up on the sidebar.</p>
<p><a title="Download Widget" href="http://wpengineer.com/?download=Single%20Post%20Navigation%20Widget">Download Widget</a>.<br />
Then unpack and copy the code into <strong>functions.php</strong>. Please note, the widget is for <strong>WordPress 2.8</strong>.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1551/wordpress-2-8-single-post-navigation-widget/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Recents Drafts All Authors</title>
		<link>http://wpengineer.com/1343/recents-drafts-all-authors/</link>
		<comments>http://wpengineer.com/1343/recents-drafts-all-authors/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 11:36:20 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[Feed]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1343</guid>
		<description><![CDATA[It's not uncommon that a blog runs by different authors, so it may be useful if you can have a quick look at the drafts of all authors. In our joint blog WP Engineer we created a feed, which keeps us up to date if a new draft of all authors were created. The work [...]]]></description>
			<content:encoded><![CDATA[<p>It's not uncommon that a blog runs by different authors, so it may be useful if you can have a quick look at the drafts of all authors. In our joint blog WP Engineer we created a feed, which keeps us up to date if a new draft of all authors were created.</p>
<p>The work of every author is different and the dashboard is the center of information. Therefore we have decided to supplement a widget in the Dashboard, which shows the last five drafts of all authors. I enhanced the existing Plugin <a href="http://wpengineer.com/feed-for-drafts-plugin/">Draft Feed</a>.</p>
<ul>
<li>It provides a feed of all drafts</li>
<li>And the last 5 drafts of all authors in the Dashboard.</li>
</ul>
<p><span id="more-1343"></span><br />
<img src="http://wpengineer.com/wp-content/uploads/recents_draft.png" alt="recents_draft" title="recents_draft" width="362" height="225" class="aligncenter size-full wp-image-1344" /></p>
<p>Everybody who needs something like this, can download this Plugin from my <a href="http://bueltge.de/wordpress-feed-fuer-entwuerfe/829/">German Blog</a>. It doesn't have any options and doesn't exist in SVN of WordPress - if you want you can use and expand it or leave a comment if you have a question. Enjoy!<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1343/recents-drafts-all-authors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WordPress 2.8 Widgets Options Page &#8211; First Screenshot!</title>
		<link>http://wpengineer.com/1124/wordpress-28-widgets-options-page/</link>
		<comments>http://wpengineer.com/1124/wordpress-28-widgets-options-page/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 10:31:57 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress News]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp2.8]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1124</guid>
		<description><![CDATA[In the current trunk of WordPress is a "first run" of the new widget management available to see. This is a development version, it shows the warning "This page is not 100% ready, please use only on test installations.". Also new is that the sidebar now saves or updates via AJAX. Here is a Screenshot [...]]]></description>
			<content:encoded><![CDATA[<p>In the current trunk of WordPress is a "first run" of the new widget management available to see. This is a development version, it shows the warning "This page is not 100% ready, please use only on test installations.".<span id="more-1124"></span></p>
<p>Also new is that the sidebar now saves or updates via AJAX. Here is a Screenshot from the current development version:</p>
<p><img class="alignnone size-full" title="WordPress 2.8 Backend Widget Options" src="http://wpengineer.com/wp-content/uploads/widgets.jpg" alt="WordPress 2.8 Backend Widget Options" width="590" height="365" /><br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1124/wordpress-28-widgets-options-page/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Build A WordPress 2.8 Widget With The New Widget API</title>
		<link>http://wpengineer.com/1023/wordpress-built-a-widget/</link>
		<comments>http://wpengineer.com/1023/wordpress-built-a-widget/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 11:00:45 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[Widget API]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp2.8]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1023</guid>
		<description><![CDATA[One of the main changes in WordPress 2.8 is the new widget API. This API is fully object oriented and provides the programmer all the necessary functions to create a WordPress widgets. Also, it now allows multiple use of each widget. You can find the widget API in wp-includes/widget.php and the widgets itself in wp-includes/default-widgets.php. [...]]]></description>
			<content:encoded><![CDATA[<p>One of the main changes in WordPress 2.8 is the new <strong>widget API</strong>. This API is fully object oriented and provides the programmer all the necessary functions to create a WordPress widgets. Also, it now allows multiple use of each widget.</p>
<p><span id="more-1023"></span></p>
<p>You can find the widget API in wp-includes/widget.php and the widgets itself in wp-includes/default-widgets.php. The class WP_Widget provides the functionality of the widgets, and each widget must be inherited. The class WP_Widget_Factory is for registration and instantiation of widgets responsible.</p>
<h3>How to create a widget in WordPress 2.8?</h3>
<p>It will be a simple example and will output the links for the RSS Feed of the articles and comments. In functions.php we first create the skeleton of our widgets:</p>
<pre lang="php">
class My_RSS_Widget extends WP_Widget {
	function My_RSS_Widget() {
		//Constructor
	}

	function widget($args, $instance) {
		// prints the widget
	}

	function update($new_instance, $old_instance) {
		//save the widget
	}

	function form($instance) {
		//widgetform in backend
	}
}
register_widget(&#039;My_RSS_Widget&#039;);
</pre>
<p>We have a class My_RSS_Widget created, which inherits the properties and methods of the WP_Widget class (extends). In a new widget, the methods <strong>widget</strong> and <strong>update</strong> always have to be created, <strong>form</strong> is optional. With <strong>register_widget</strong> ( 'My_RSS_Widget') will be the widget registered. Let's fill up our widget.</p>
<pre lang="php">
class My_RSS_Widget extends WP_Widget {
	function My_RSS_Widget() {
		$widget_ops = array(&#039;classname&#039; =&gt; &#039;widget_rss_links&#039;, &#039;description&#039; =&gt; &#039;A list with your feeds links&#039; );
		$this-&gt;WP_Widget(&#039;rss_links&#039;, &#039;Feed links&#039;, $widget_ops);
	}

	function widget($args, $instance) {
		extract($args, EXTR_SKIP);

		echo $before_widget;
		$title = empty($instance&#091;&#039;title&#039;&#093;) ? &#039;&nbsp;&#039; : apply_filters(&#039;widget_title&#039;, $instance&#091;&#039;title&#039;&#093;);
		$entry_title = empty($instance&#091;&#039;entry_title&#039;&#093;) ? &#039;&nbsp;&#039; : apply_filters(&#039;widget_entry_title&#039;, $instance&#091;&#039;entry_title&#039;&#093;);
		$comments_title = empty($instance&#091;&#039;comments_title&#039;&#093;) ? &#039;&nbsp;&#039; : apply_filters(&#039;widget_comments_title&#039;, $instance&#091;&#039;comments_title&#039;&#093;);

		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
		echo &#039;&lt;ul id=&quot;rss&quot;&gt;&#039;;
		echo &#039;  &lt;li&gt;&lt;a href=&quot; &#039; . get_bloginfo(&#039;rss2_url&#039;) . &#039;&quot; rel=&quot;nofollow&quot; title=&quot; &#039; . $entry_title . &#039; &quot;&gt;&#039; . $entry_title . &#039;&lt;/a&gt;&lt;/li&gt;&#039;;
		echo &#039;  &lt;li&gt;&lt;a href=&quot; &#039; . get_bloginfo(&#039;comments_rss2_url&#039;) . &#039;&quot; rel=&quot;nofollow&quot; title=&quot;  &#039;. $comments_title . &#039; &quot;&gt;&#039; . $comments_title . &#039;&lt;/a&gt;&lt;/li&gt;&#039;;
		echo &#039;&lt;/ul&gt;&#039;;
		echo $after_widget;
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance&#091;&#039;title&#039;&#093; = strip_tags($new_instance&#091;&#039;title&#039;&#093;);
		$instance&#091;&#039;entry_title&#039;&#093; = strip_tags($new_instance&#091;&#039;entry_title&#039;&#093;);
		$instance&#091;&#039;comments_title&#039;&#093; = strip_tags($new_instance&#091;&#039;comments_title&#039;&#093;);

		return $instance;
	}

	function form($instance) {
		$instance = wp_parse_args( (array) $instance, array( &#039;title&#039; =&gt; &#039;&#039;, &#039;entry_title&#039; =&gt; &#039;&#039;, &#039;comments_title&#039; =&gt; &#039;&#039; ) );
		$title = strip_tags($instance&#091;&#039;title&#039;&#093;);
		$entry_title = strip_tags($instance&#091;&#039;entry_title&#039;&#093;);
		$comments_title = strip_tags($instance&#091;&#039;comments_title&#039;&#093;);
?&gt;
			&lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;title&#039;); ?&gt;&quot;&gt;Title: &lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;title&#039;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#039;title&#039;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo attribute_escape($title); ?&gt;&quot; /&gt;&lt;/label&gt;&lt;/p&gt;
			&lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;entry_title&#039;); ?&gt;&quot;&gt;Title for entry feed: &lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;entry_title&#039;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#039;entry_title&#039;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo attribute_escape($entry_title); ?&gt;&quot; /&gt;&lt;/label&gt;&lt;/p&gt;
			&lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;comments_title&#039;); ?&gt;&quot;&gt;Title for comments feed: &lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;comments_title&#039;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#039;comments_title&#039;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo attribute_escape($comments_title); ?&gt;&quot; /&gt;&lt;/label&gt;&lt;/p&gt;
&lt;?php
	}
}
register_widget(&#039;My_RSS_Widget&#039;);
</pre>
<p><img src="http://wpengineer.com/wp-content/uploads/rss-widget-backend.jpg" alt="RSS Widget Backend" title="RSS Widget Backend" width="286" height="321" class="aligncenter size-full wp-image-1025" /></p>
<p> The widget feature is for the output in the frontend to examine the update data and stores an instance of the widget and form creates in the backend the input mask, which in our case for the title of the widget and the displayed link text.</p>
<p>The code is self-explanatory. The function <strong>widget</strong> is for the output in the frontend responsible, <strong>update</strong> examine the data and stores an instance of the widget and <strong>form</strong> creates in the backend the input mask, which in our case for the title of the widget and the displayed link text.</p>
<p><img src="http://wpengineer.com/wp-content/uploads/rss-widget-frontend.jpg" alt="RSS Widget frontend" title="RSS Widget frontend" width="269" height="121" class="aligncenter size-full wp-image-1026" /></p>
<p>So now implement your own ideas!</p>
<p><strong>Please check out the post <a href="http://wpengineer.com/wordpress-2-8-single-post-navigation-widget/">WordPress 2.8 Single Post Navigation Widget</a> for a more advanced example.</strong><br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1023/wordpress-built-a-widget/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
		<item>
		<title>WordPress and register_sidebar</title>
		<link>http://wpengineer.com/657/wordpress-and-register_sidebar/</link>
		<comments>http://wpengineer.com/657/wordpress-and-register_sidebar/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 16:14:20 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=657</guid>
		<description><![CDATA[Everyone who is building WordPress Themes probably implement the widget function in their theme. Code for the widgets is in functions.php. Here a code snippet of the default theme: if ( function_exists(&#039;register_sidebar&#039;) ) register_sidebar(array( &#039;before_widget&#039; =&#62; &#039;&#60;li id=&#34;%1$s&#34; class=&#34;widget %2$s&#34;&#62;&#039;, &#039;after_widget&#039; =&#62; &#039;&#60;/li&#62;&#039;, &#039;before_title&#039; =&#62; &#039;&#60;h2 class=&#34;widgettitle&#34;&#62;&#039;, &#039;after_title&#039; =&#62; &#039;&#60;/h2&#62;&#039;, )); As we all know, [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone who is building WordPress Themes probably implement the widget function in their theme. Code for the widgets is in functions.php. Here a code snippet of the default theme:</p>
<p><span id="more-657"></span></p>
<pre lang="php">
if ( function_exists(&#039;register_sidebar&#039;) )
    register_sidebar(array(
        &#039;before_widget&#039; =&gt; &#039;&lt;li id=&quot;%1$s&quot; class=&quot;widget %2$s&quot;&gt;&#039;,
        &#039;after_widget&#039; =&gt; &#039;&lt;/li&gt;&#039;,
        &#039;before_title&#039; =&gt; &#039;&lt;h2 class=&quot;widgettitle&quot;&gt;&#039;,
        &#039;after_title&#039; =&gt; &#039;&lt;/h2&gt;&#039;,
    ));
</pre>
<p><!--more--></p>
<p>As we all know, there is the function <strong>register_sidebar</strong> since version 2.2. My question is: <strong>How long do you want to ask if this function exists? </strong> The default theme of WordPress 2.7 doesn't ask, if <strong>have_comments</strong> (a new template tag of version 2.7) exists, but it asks if register_sidebar exists. I just suppose the developers just forgot it and everybody in the world thinks, that it has to be like that.</p>
<p>Please don't misunderstand, the query isn't wrong, but in my opinion just useless in the default theme of version 2.7</p>
<p>Since we are already talking about the register_sidebar and it's use, the code of a premium theme uses this query even 3 times:</p>
<pre lang="php">
&lt;?php
if ( function_exists(&#039;register_sidebar&#039;) )
register_sidebar(array(&#039;name&#039;=&gt;&#039;Home Sidebar&#039;,
&#039;before_widget&#039; =&gt; &#039;&lt;div class=&quot;block&quot;&gt;&#039;,
&#039;after_widget&#039; =&gt; &#039;&lt;/div&gt;&lt;div class=&quot;blockfooter&quot;&gt;&lt;/div&gt;&#039;,
&#039;before_title&#039; =&gt; &#039;&lt;h3&gt;&#039;,
&#039;after_title&#039; =&gt; &#039;&lt;/h3&gt;&#039;,
));
?&gt;
&lt;?php
if ( function_exists(&#039;register_sidebar&#039;) )
register_sidebar(array(&#039;name&#039;=&gt;&#039;Post Sidebar&#039;,
&#039;before_widget&#039; =&gt; &#039;&lt;div class=&quot;block&quot;&gt;&#039;,
&#039;after_widget&#039; =&gt; &#039;&lt;/div&gt;&lt;div class=&quot;blockfooter&quot;&gt;&lt;/div&gt;&#039;,
&#039;before_title&#039; =&gt; &#039;&lt;h3&gt;&#039;,
&#039;after_title&#039; =&gt; &#039;&lt;/h3&gt;&#039;,
));
?&gt;
&lt;?php
if ( function_exists(&#039;register_sidebar&#039;) )
register_sidebar(array(&#039;name&#039;=&gt;&#039;Page Sidebar&#039;,
&#039;before_widget&#039; =&gt; &#039;&lt;div class=&quot;block&quot;&gt;&#039;,
&#039;after_widget&#039; =&gt; &#039;&lt;/div&gt;&lt;div class=&quot;blockfooter&quot;&gt;&lt;/div&gt;&#039;,
&#039;before_title&#039; =&gt; &#039;&lt;h3&gt;&#039;,
&#039;after_title&#039; =&gt; &#039;&lt;/h3&gt;&#039;,
));
?&gt;
</pre>
<p>This is not really a good example for a premium theme. First, why closing the PHP tag after every function and open it before every function again?<br />
Second, if the function register_sidebar exist in first place, it will also exist the 2nd time<br />
and third, the code is almost identical except the name of the sidebar. That could be done much easier:</p>
<pre lang="php">
&lt;?php
$sidebars = array(&#039;Home Sidebar&#039;, &#039;Post Sidebar&#039;, &#039;Page Sidebar&#039;);
foreach($sidebars as $sb) {
	register_sidebar(array(&#039;name&#039;=&gt; $sb,
		&#039;before_widget&#039; =&gt; &#039;&lt;div class=&quot;block&quot;&gt;&#039;,
		&#039;after_widget&#039; =&gt; &#039;&lt;/div&gt;&lt;div class=&quot;blockfooter&quot;&gt;&lt;/div&gt;&#039;,
		&#039;before_title&#039; =&gt; &#039;&lt;h3&gt;&#039;,
		&#039;after_title&#039; =&gt; &#039;&lt;/h3&gt;&#039;,
	));
}
?&gt;
</pre>
<p>As I said, it's not wrong and I didn't mean it as it is a bad way to code, but sometimes you can do it much easier <img src='http://wpengineer.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/657/wordpress-and-register_sidebar/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

