<?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>Digging into WordPress &#187; PHP</title>
	<atom:link href="http://digwp.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Learn how to take your WordPress skills to the next level.</description>
	<lastBuildDate>Mon, 22 Mar 2010 06:25:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Declare Multiple Widgetized&#160;Areas</title>
		<link>http://digwp.com/2010/03/declare-multiple-widgetized-areas/</link>
		<comments>http://digwp.com/2010/03/declare-multiple-widgetized-areas/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 12:24:26 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1686</guid>
		<description><![CDATA[Have a bunch of different areas you wish to declare as a widgetized area? Save repetative code by creating a quick array of their names, then loop through that array calling the register_sidebar() function on each one. Elementary PHP stuff here, but hey, it just saved me quite a few lines of code in a [...]]]></description>
			<content:encoded><![CDATA[<p>Have a bunch of different areas you wish to declare as a widgetized area? Save repetative code by creating a quick array of their names, then loop through that array calling the register_sidebar() function on each one. Elementary PHP stuff here, but hey, it just saved me quite a few lines of code in a widget-heavy theme I am working on.</p>
<pre><code>if ( function_exists('register_sidebar') ) {

    $allWidgetizedAreas = array("Homepage Left", "Homepage Right", "Sidebar One", "Movies", "Admin");
    
    foreach ($allWidgetizedAreas as $WidgetAreaName) {
    
        register_sidebar(array(
           'name'=&gt; $WidgetAreaName,
           'before_widget' =&gt; '&lt;div id="%1$s" class="widget %2$s left half"&gt;',
           'after_widget' =&gt; '&lt;/div&gt;',
           'before_title' =&gt; '&lt;h3 class="widgettitle"&gt;',
           'after_title' =&gt; '&lt;/h3&gt;',
        ));
    
    }

}</code></pre>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/03/declare-multiple-widgetized-areas/">Permalink</a> | <a href="http://digwp.com/2010/03/declare-multiple-widgetized-areas/#comments">3 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/03/declare-multiple-widgetized-areas/&title=Declare Multiple Widgetized&nbsp;Areas">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/widgets/" rel="tag">widgets</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/03/declare-multiple-widgetized-areas/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Add Classes to&#160;post_class</title>
		<link>http://digwp.com/2010/03/add-classes-to-post_class/</link>
		<comments>http://digwp.com/2010/03/add-classes-to-post_class/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 21:04:45 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[Theme]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1567</guid>
		<description><![CDATA[The post_class() function in WordPress is pretty darn useful. It is used like this, in most templates, in a wrapping div of all the content you are outputting:
&#60;div &#60;?php post_class() ?&#62; id="post-&#60;?php the_ID(); ?&#62;"&#62;
   &#60;!-- Post stuff --&#62;
&#60;/div&#62;
I was in a circumstance where I wanted to add an additional class to what that [...]]]></description>
			<content:encoded><![CDATA[<p>The post_class() function in WordPress is pretty darn useful. It is used like this, in most templates, in a wrapping div of all the content you are outputting:</p>
<pre><code>&lt;div &lt;?php post_class() ?&gt; id="post-&lt;?php the_ID(); ?&gt;"&gt;
   &lt;!-- Post stuff --&gt;
&lt;/div&gt;</code></pre>
<p>I was in a circumstance where I wanted to add an additional class to what that was outputting. My thought process went like this:</p>
<ol>
<li>Arg. I guess I&#8217;m going to have to write a function to filter the output of that function. Let&#8217;s begin the research.</li>
<li>Wait. I bet that function has an alternate that returns a string rather than echos it. I&#8217;ll just use that, and echo it out along with my own custom class.</li>
<li>Double wait. I bet those smart WordPress folks have thought of this&#8230;</li>
</ol>
<p><span id="more-1567"></span></p>
<h3>1. Filtering</h3>
<p>This kind of thing is generally beyond my comfort zone, but you&#8217;d basically write your own little function which takes in what the output would have been, add you own stuff to it, then return the new value.</p>
<p>Then call the new function with a filter in your functions.php file:</p>
<pre><code>add_filter('post_class','add_my_stuff');</code></pre>
<h3>2. Output exiting classes, then add yours</h3>
<p>Like most functions that echo stuff, there is a version that doesn&#8217;t echo stuff, in which &#8220;get_&#8221; prefaces the function name. I didn&#8217;t even have to look it up I just knew there would be. Since it&#8217;s a list, this function returns an array, so to output them, you&#8217;ll need to loop through the array:</p>
<pre><code>&lt;div class="&lt;?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } ?&gt; group" id="post-&lt;?php the_ID(); ?&gt;"&gt;</code></pre>
<p>The cool part here is that is doesn&#8217;t add &#8220;class=&#8221; in the output, you can do that yourself, meaning that I can add my own classes after the output is finished.</p>
<h3>3. Built in&#8230;</h3>
<p>Of course, as it turns out, this is far easier than #2 made it out to be. To add additional classes, just pass them as a parameter!</p>
<pre><code>&lt;?php post_class('special'); ?&gt;</code></pre>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/03/add-classes-to-post_class/">Permalink</a> | <a href="http://digwp.com/2010/03/add-classes-to-post_class/#comments">2 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/03/add-classes-to-post_class/&title=Add Classes to&nbsp;post_class">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/classes/" rel="tag">classes</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/theme/" rel="tag">Theme</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/03/add-classes-to-post_class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Shortcodes in&#160;Widgets</title>
		<link>http://digwp.com/2010/03/shortcodes-in-widgets/</link>
		<comments>http://digwp.com/2010/03/shortcodes-in-widgets/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 13:07:32 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1560</guid>
		<description><![CDATA[I had occasion the other day to run a shortcode inside of a text widget. You know shortcodes&#8230; we talk about them all the time. They are keywords in [square brackets] that do something special. Sometimes something really simple like returning a string (so you can have a global location to change that string) or [...]]]></description>
			<content:encoded><![CDATA[<p>I had occasion the other day to run a shortcode inside of a text widget. You know shortcodes&#8230; we talk about them <a href="http://digwp.com/tag/shortcode/">all the time</a>. They are keywords in [square brackets] that do something special. Sometimes something really simple like returning a string (so you can have a global location to change that string) or something complicated like call a plugin that does something fancy like build a photo gallery.</p>
<p>But alas&#8230;</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/blog-images/shortcodeinwidget.png" width="371" height="222" alt="" title="" /><br />
This ain&#8217;t gonna work &#8220;out-of-the-box&#8221;
</div>
<p><span id="more-1560"></span></p>
<p>After tinkering with some far-too-complicated solutions, someone on Twitter helped me with a deliciously simple solution: Just add this to your functions.php file:</p>
<pre><code>add_filter('widget_text', 'do_shortcode');</code></pre>
<p>This will ensure that the text content of widgets is parsed for shortcodes and those shortcodes are ran. Awesome.</p>
<p>Stephanie Leary wrote a great article on <a href="http://sillybean.net/wordpress/content/using-shortcodes-everywhere/">Using Shortcodes Everywhere</a>, which covers this as well as using shortcodes in a bunch of other places: Comments, templates, excerpts, etc.</p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/03/shortcodes-in-widgets/">Permalink</a> | <a href="http://digwp.com/2010/03/shortcodes-in-widgets/#comments">4 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/03/shortcodes-in-widgets/&title=Shortcodes in&nbsp;Widgets">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a>, <a href="http://digwp.com/tag/widgets/" rel="tag">widgets</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/03/shortcodes-in-widgets/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Display Separate Counts for Comments, Pingbacks and&#160;Trackbacks</title>
		<link>http://digwp.com/2010/02/separate-comments-pingbacks-trackbacks/</link>
		<comments>http://digwp.com/2010/02/separate-comments-pingbacks-trackbacks/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 09:43:48 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1452</guid>
		<description><![CDATA[In WordPress, there are three ways to respond to a post: you can leave a comment, leave a trackback, or just link to the post to create a pingback. When displaying all of the responses to your posts, it&#8217;s a good idea to separate the comments from the pingbacks and trackbacks. Uninterrupted comment threads are [...]]]></description>
			<content:encoded><![CDATA[<p>In WordPress, there are three ways to respond to a post: you can leave a comment, leave a trackback, or just link to the post to create a pingback. When displaying all of the responses to your posts, it&rsquo;s a good idea to separate the comments from the pingbacks and trackbacks. Uninterrupted comment threads are a pleasure to read, as are well-styled lists of pingbacks. This is an excellent way to improve the usability, organization, and stylishness of your comment areas. </p>
<p>In this <acronym title="Digging into WordPress">DiW</acronym> tutorial, you&rsquo;ll learn how to separate your responses and display the comment count for each type. Here is a diagram to help visual the goal:</p>
<p><img src="http://digwp.com/wp-content/blog-images/comments-pingbacks.gif" alt="[ Separated display of comments, pingbacks and trackbacks ]" /></p>
<p><span id="more-1452"></span></p>
<p>As you can see, separating these lists gives us a cleaner comments area, making things easier for everyone. Even better news is that we&rsquo;re going to set it up in <strong>two steps</strong>. If you would rather group the pingbacks and trackbacks and just segregate the comments, just a few tweaks and you&rsquo;re off to the races.</p>
<p>First let&rsquo;s see how to separate everything, then explain the grouping of pingbacks and trackbacks before wrapping things up.</p>
<h3>Step 1 &#8211; Display separate comments, pingbacks, and trackbacks</h3>
<p>The first thing we need to do is separate the different response types. Fortunately, WordPress makes this very easy. First, open your them&rsquo;s <code>single.php</code> file and use the following template tag to call your <code>comments.php</code> template:</p>
<pre><code>comments_template('/comments.php',true);</code></pre>
<p>This code will enable WordPress to segregate and count each of the different response types using the <code>$wp_query</code> object. Now let&rsquo;s separate the comments, pingbacks, and trackbacks. In your <code>comments.php</code> file, use the following code as your comments loop:</p>
<pre><code>&lt;?php if (!empty($comments_by_type['comment'])) { ?&gt;

	&lt;h3 id="comments"&gt;Comments&lt;h3&gt;
	&lt;ol class="commentlist"&gt;
		&lt;?php wp_list_comments('type=comment'); ?&gt;
	&lt;/ol&gt;

&lt;?php } if (!empty($comments_by_type['pingback'])) { ?&gt;

	&lt;h3 id="pingbacks"&gt;Pingbacks&lt;/h3&gt;
	&lt;ol class="pingbacklist"&gt;
		&lt;?php wp_list_comments('type=pingback'); ?&gt;
	&lt;/ol&gt;

&lt;?php } if (!empty($comments_by_type['trackback'])) { ?&gt;

	&lt;h3 id="trackbacks"&gt;Trackbacks&lt;/h3&gt;
	&lt;ol class="trackbacklist"&gt;
		&lt;?php wp_list_comments('type=trackback'); ?&gt;
	&lt;/ol&gt;

&lt;?php } ?&gt;</code></pre>
<p>This symmetrical slice of code will output the following markup, assuming that the post actually has all three types of responses:</p>
<pre><code>&lt;h3 id="comments"&gt;Comments&lt;h3&gt;
&lt;ol class="commentlist"&gt;
	&lt;li&gt;Comment #1 - Blah blah blah..&lt;/li&gt;
	&lt;li&gt;Comment #2 - Blah blah blah..&lt;/li&gt;
	&lt;li&gt;Comment #3 - Blah blah blah..&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="pingbacks"&gt;Pingbacks&lt;/h3&gt;
&lt;ol class="pingbacklist"&gt;
	&lt;li&gt;Pingback #1 - Blah blah blah..&lt;/li&gt;
	&lt;li&gt;Pingback #2 - Blah blah blah..&lt;/li&gt;
	&lt;li&gt;Pingback #3 - Blah blah blah..&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="trackbacks"&gt;Trackbacks&lt;/h3&gt;
&lt;ol class="trackbacklist"&gt;
	&lt;li&gt;Trackback #1 - Blah blah blah..&lt;/li&gt;
	&lt;li&gt;Trackback #2 - Blah blah blah..&lt;/li&gt;
	&lt;li&gt;Trackback #3 - Blah blah blah..&lt;/li&gt;
&lt;/ol&gt;</code></pre>
<p>When one of the different response types doesn&rsquo;t exist on the post, that section will simply be omitted from the markup, keeping things nice and clean. </p>
<h3>Step 2 &#8211; Display separate response counts</h3>
<p>Now, to display the count for each of these response types, we can call upon the <code>$wp_query</code> object to retrieve and echo the information:</p>
<pre><code>&lt;?php echo count($wp_query-&gt;comments_by_type['comments']); ?&gt;
&lt;?php echo count($wp_query-&gt;comments_by_type['pingback']); ?&gt;
&lt;?php echo count($wp_query-&gt;comments_by_type['trackback']); ?&gt;</code></pre>
<p>Each of these lines displays a number corresponding to a specific response type. These response counts may be integrated into our previous code to give us the final result:</p>
<pre><code>&lt;?php if (have_comments()) : global $wp_query; ?&gt;

	&lt;h2 id="comments"&gt;&lt;?php comments_number('No Responses', 'One Response', '% Responses' ); ?&gt;&lt;h2&gt;

	&lt;?php if (!empty($comments_by_type['comment'])) { ?&gt;

		&lt;h3 id="comments"&gt;&lt;?php echo count($wp_query-&gt;comments_by_type['comment']); ?&gt; Comments&lt;h3&gt;
		&lt;ol class="commentlist"&gt;
			&lt;?php wp_list_comments('type=comment'); ?&gt;
		&lt;/ol&gt;

	&lt;?php } ?&gt;
	&lt;?php if (!empty($comments_by_type['pingback'])) { ?&gt;

		&lt;h3 id="pingbacks"&gt;&lt;?php echo count($wp_query-&gt;comments_by_type['pingback']); ?&gt; Pingbacks&lt;/h3&gt;
		&lt;ol class="pingbacklist"&gt;
			&lt;?php wp_list_comments('type=pingback'); ?&gt;
		&lt;/ol&gt;

	&lt;?php } ?&gt;
	&lt;?php if (!empty($comments_by_type['trackback'])) { ?&gt;

		&lt;h3 id="trackbacks"&gt;&lt;?php echo count($wp_query-&gt;comments_by_type['trackback']); ?&gt; Trackbacks&lt;/h3&gt;
		&lt;ol class="trackbacklist"&gt;
			&lt;?php wp_list_comments('type=trackback'); ?&gt;
		&lt;/ol&gt;

	&lt;?php } ?&gt;

&lt;?php else : // if there are no comments yet ?&gt;

	&lt;?php if (comments_open()) : ?&gt;
		&lt;!-- comments open, no comments --&gt;
	 &lt;?php else : ?&gt;
		&lt;!-- comments closed, no comments --&gt;
	&lt;?php endif; ?&gt;

&lt;?php endif; ?&gt;</code></pre>
<p>That&rsquo;s the magic bullet right there. Used as your comments loop, this code will display separate comments, pingbacks, and trackbacks, with each showing their respective number of responses in its heading. Notice we threw in some additional code there to make everything completely plug-n-play. Bottom line: slap it in and test it yourself. Should work perfectly for all WordPress 2.7 and better.</p>
<h3>Separate comments from both pingbacks and trackbacks</h3>
<p>Note that, alternately, you could segregate comments from both pingbacks <em>and</em> trackbacks, such that your comments display would show something like this:</p>
<p><img src="http://digwp.com/wp-content/blog-images/comments-trackbacks.gif" alt="[ Separated display of comments from both pingbacks and trackbacks ]" /></p>
<p>To do this, we modify the comment loop (from Step 1 above) like so:</p>
<pre><code>&lt;?php if (!empty($comments_by_type['comment'])) { ?&gt;

	&lt;h3 id="comments"&gt;Comments&lt;h3&gt;
	&lt;ol class="commentlist"&gt;
		&lt;?php wp_list_comments('type=comment'); ?&gt;
	&lt;/ol&gt;

&lt;?php } if (!empty($comments_by_type['pings'])) { ?&gt;

	&lt;h3 id="trackbacks"&gt;Pingbacks &amp;amp; Trackbacks&lt;/h3&gt;
	&lt;ol class="ping-trackbacklist"&gt;
		&lt;?php wp_list_comments('type=pings'); ?&gt;
	&lt;/ol&gt;

&lt;?php } ?&gt;</code></pre>
<p>Grouping the pingbacks and trackbacks is easy thanks to the &ldquo;<code>pings</code>&rdquo; parameter for the second <code>wp_list_comments</code> loop. Then to display the count for our combined pingbacks and trackbacks, we insert the following snippet into our &ldquo;Pingbacks &amp; Trackbacks&rdquo; heading (similar to Step 2 above):</p>
<pre><code>&lt;?php echo count($wp_query-&gt;comments_by_type['pings']); ?&gt;</code></pre>
<p>That&rsquo;s all there is to it! Once you get everything in place and working properly, you&rsquo;ll have some clean <acronym title="Hypertext Markup Language">HTML</acronym> canvas to paint with any <acronym title="Cascading Style Sheets">CSS</acronym> styles you wish. Sounds like a great way to spend some time&nbsp;;)</p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/02/separate-comments-pingbacks-trackbacks/">Permalink</a> | <a href="http://digwp.com/2010/02/separate-comments-pingbacks-trackbacks/#comments">12 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/02/separate-comments-pingbacks-trackbacks/&title=Display Separate Counts for Comments, Pingbacks and&nbsp;Trackbacks">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/design/" title="View all posts in Design" rel="category tag">Design</a>,  <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/comments/" rel="tag">comments</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/02/separate-comments-pingbacks-trackbacks/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to Widgetize Your WordPress Theme in 2&#160;Steps</title>
		<link>http://digwp.com/2010/02/how-to-widgetize-wordpress-theme/</link>
		<comments>http://digwp.com/2010/02/how-to-widgetize-wordpress-theme/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 17:47:30 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1379</guid>
		<description><![CDATA[Working on a new theme for the next Digging into WordPress book update, I found myself really getting into the whole &#8220;widgetizing&#8221; thing. Widgets enable non-technical users to customize your theme according to their specific layout needs, and with so many different widgets available, the possibilities are endless. You may have thought about widgets as [...]]]></description>
			<content:encoded><![CDATA[<p>Working on a new theme for the next Digging into WordPress book update, I found myself really getting into the whole &ldquo;widgetizing&rdquo; thing. Widgets enable non-technical users to customize your theme according to their specific layout needs, and with <a href="http://codex.wordpress.org/WordPress_Widgets" title="WordPress Widgets">so many</a> different widgets available, the possibilities are endless. You may have thought about widgets as something you do in the sidebar, but there is no reason to stop there. You can widgetize just about every part of your theme. In this post, we&rsquo;ll show you how to do widgetize your theme in <strong>two easy steps</strong>. Once we get the basics down, we&rsquo;ll dig into some <em>sweet tips and tricks</em>.</p>
<p><span id="more-1379"></span></p>
<h3>Widgetize Your WordPress Theme: Step 1</h3>
<p>Make a fist and howl at the monitor. Then place this code into your active theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>if (function_exists('register_sidebar')) {

	register_sidebar(array(
		'name' =&gt; 'Widgetized Area',
		'id'   =&gt; 'widgetized-area',
		'description'   =&gt; 'This is a widgetized area.',
		'before_widget' =&gt; '&lt;div id="%1$s" class="widget %2$s"&gt;',
		'after_widget'  =&gt; '&lt;/div&gt;',
		'before_title'  =&gt; '&lt;h4&gt;',
		'after_title'   =&gt; '&lt;/h4&gt;'
	));

}</code></pre>
<p>This is all that is needed for the first step. Once this widgetizing code is placed in your <code>functions.php</code> file, proceed to Step 2. The remainder of this section is explanation.</p>
<p>This code checks to make sure that the current version of WordPress supports widgets, and then declares an array of values that will be used to create the widgetized area in your theme. Here are the different values:</p>
<ul>
<li><code>name</code> &ndash; the name of the widgetized area as displayed in the <acronym title="WordPress">WP</acronym> Admin</li>
<li><code>id</code> &ndash; a unique identifier for your widgetized area</li>
<li><code>description</code> &ndash; description of the widgetized area</li>
<li><code>before_widget</code> &ndash; the markup generated before any user-added widgets</li>
<li><code>after_widget</code> &ndash; the markup generated after any user-added widgets</li>
<li><code>before_title</code> &ndash; the markup generated before the title of any user-added widgets</li>
<li><code>after_title</code> &ndash; the markup generated after the title of any user-added widgets</li>
</ul>
<p>So, given these parameters, our widgetizing code would result in the following output, say, if the built-in Search widget were added to our widgetized area:</p>
<pre><code>&lt;div id="search-3" class="widget widget_search"&gt;
	&lt;h4&gt;Search&lt;/h4&gt;
	&lt;form role="search" method="get" id="searchform" action="http://localhost/283/" &gt;
		&lt;div&gt;
			&lt;label class="screen-reader-text" for="s"&gt;Search for:&lt;/label&gt;
			&lt;input type="text" value="" name="s" id="s" /&gt;
			&lt;input type="submit" id="searchsubmit" value="Search" /&gt;
		&lt;/div&gt;
	&lt;/form&gt;
&lt;/div&gt;</code></pre>
<p>Note the markup created for the opening <code>&lt;div&gt;</code>, which gets its attribute information based on the wild-card matching specified in our widgetizing array. To see this more clearly, examine the following side-by-side comparison of the <code>sprintf</code> directives and the resulting <acronym title="(eXtensible) Hypertext Markup Language">(X)HTML</acronym> source code:</p>
<pre><code>'before_widget' =&gt; '&lt;div id="%1$s" class="widget %2$s"&gt;',
&lt;div id="search-3" class="widget widget_search"&gt;</code></pre>
<p>Hopefully the relationship is clear. This is a powerful way to ensure that all widgets have a similar class names and unique <acronym title="Identification">ID</acronym>s, which are important for easy <acronym title="Cascading Style Sheets">CSS</acronym> styles.</p>
<p>Let&rsquo;s continue with Step 2..</p>
<h3>Widgetize Your WordPress Theme: Step 2</h3>
<p>Finally, add the following code to the location in your theme&rsquo;s template file(s) where you would like the widgetized area to appear:</p>
<pre><code>&lt;div id="widgetized-area"&gt;

	&lt;?php if (function_exists('dynamic_sidebar') &amp;&amp; dynamic_sidebar('widgetized-area')) : else : ?&gt;

	&lt;div class="pre-widget"&gt;
		&lt;p&gt;&lt;strong&gt;Widgetized Area&lt;/strong&gt;&lt;/p&gt;
		&lt;p&gt;This panel is active and ready for you to add some widgets via the WP Admin&lt;/p&gt;
	&lt;/div&gt;

	&lt;?php endif; ?&gt;

&lt;/div&gt;</code></pre>
<p>With both steps complete, your theme should now feature a widgetized area in the location of your choosing. Nothing else needs to be done at this point. If you get it, then get to it and continue developing your theme. For more information and some juicy tips and tricks, continue reading.</p>
<p>Let&rsquo;s examine the code used in this second step. Everything located between the <code>if</code>/<code>endif</code> statements will be output to the browser when no widgets have been activated. This is a good place to inform the user that the area is widgetized and may be customized via the Widgets area in the WordPress Admin. I also include a &ldquo;<code>pre-widget</code>&rdquo; class to help style the pre-widget area. With multiple widgetized areas on a page, having a class name to hang styles on is super nice.</p>
<p>Also, note that we are using the <code>id</code> specified in the widgetizing array (refer to Step 1) as the parameter value for the specific <code>dynamic_sidebar()</code> check in the third line.</p>
<h3>Multiple widgetized areas</h3>
<p>So we now have a nice, widgetized sidebar. But why stop there? Implementing additional widgetized areas is as simple as replicating the code in Step 1 and Step 2. For example, let&rsquo;s say we wanted to widgetize the header, sidebar, and footer areas of our theme. We would be put this in our <code>functions.php</code> file:</p>
<pre><code>if (function_exists('register_sidebar')) {

	register_sidebar(array(
		'name' =&gt; 'Header',
		'id'   =&gt; 'header',
		'description'   =&gt; 'This is the widgetized header.',
		'before_widget' =&gt; '&lt;div id="%1$s" class="widget %2$s"&gt;',
		'after_widget'  =&gt; '&lt;/div&gt;',
		'before_title'  =&gt; '&lt;h4&gt;',
		'after_title'   =&gt; '&lt;/h4&gt;'
	));
	register_sidebar(array(
		'name' =&gt; 'Sidebar',
		'id'   =&gt; 'sidebar',
		'description'   =&gt; 'This is the widgetized sidebar.',
		'before_widget' =&gt; '&lt;div id="%1$s" class="widget %2$s"&gt;',
		'after_widget'  =&gt; '&lt;/div&gt;',
		'before_title'  =&gt; '&lt;h4&gt;',
		'after_title'   =&gt; '&lt;/h4&gt;'
	));
	register_sidebar(array(
		'name' =&gt; 'Footer',
		'id'   =&gt; 'footer',
		'description'   =&gt; 'This is the widgetized footer.',
		'before_widget' =&gt; '&lt;div id="%1$s" class="widget %2$s"&gt;',
		'after_widget'  =&gt; '&lt;/div&gt;',
		'before_title'  =&gt; '&lt;h4&gt;',
		'after_title'   =&gt; '&lt;/h4&gt;'
	));

}</code></pre>
<p>..and then we would place the following code chunks into their respective locations within our theme template files:</p>
<pre><code>&lt;div id="widgetized-header"&gt;

	&lt;?php if (function_exists('dynamic_sidebar') &amp;&amp; dynamic_sidebar('header')) : else : ?&gt;

	&lt;div class="pre-widget"&gt;
		&lt;p&gt;&lt;strong&gt;Widgetized Header&lt;/strong&gt;&lt;/p&gt;
		&lt;p&gt;This panel is active and ready for you to add some widgets via the WP Admin&lt;/p&gt;
	&lt;/div&gt;

	&lt;?php endif; ?&gt;

&lt;/div&gt;
&lt;div id="widgetized-sidebar"&gt;

	&lt;?php if (function_exists('dynamic_sidebar') &amp;&amp; dynamic_sidebar('sidebar')) : else : ?&gt;

	&lt;div class="pre-widget"&gt;
		&lt;p&gt;&lt;strong&gt;Widgetized Sidebar&lt;/strong&gt;&lt;/p&gt;
		&lt;p&gt;This panel is active and ready for you to add some widgets via the WP Admin&lt;/p&gt;
	&lt;/div&gt;

	&lt;?php endif; ?&gt;

&lt;/div&gt;
&lt;div id="widgetized-footer"&gt;

	&lt;?php if (function_exists('dynamic_sidebar') &amp;&amp; dynamic_sidebar('footer')) : else : ?&gt;

	&lt;div class="pre-widget"&gt;
		&lt;p&gt;&lt;strong&gt;Widgetized Footer&lt;/strong&gt;&lt;/p&gt;
		&lt;p&gt;This panel is active and ready for you to add some widgets via the WP Admin&lt;/p&gt;
	&lt;/div&gt;

	&lt;?php endif; ?&gt;

&lt;/div&gt;</code></pre>
<p>That&rsquo;s all there is to it. Of course, you&rsquo;ll probably want to tweak the details to suit your specific needs, but these two steps are all you need to widgetize your WordPress theme. Now let&rsquo;s wrap things up with some juicy widgetizing tricks..</p>
<h3>Some Juicy Widgetizing Tricks</h3>
<p>Here are some sweet little tips for widgetizing your theme:</p>
<h4>Streamline &amp; organize custom widgets</h4>
<p>The first thing that I do after widgetizing a new theme is to include a separate &ldquo;<code>widgets.php</code>&rdquo; that contains all of the custom widgets. A simple line of code is all that is required to acheive this functionality:</p>
<p><code>if ($wp_version &gt;= 2.8) require_once(TEMPLATEPATH.'/widgets.php');</code></p>
<p>Place that code into your <code>functions.php</code> file and place all of your widgets into a file called <code>widgets.php</code>. Once in place, this method will ensure that your widgets are loaded and available for all supportive versions of WordPress. This is a great way to keep your theme files nice and organized.</p>
<h4>Replace WordPress&rsquo; default widgets</h4>
<p>Many of the default WordPress widgets leave something to be desired. Fortunately, it&rsquo;s super-easy to override any of the default widgets with your own creations. For example, here is one way to replace the default Search Widget with your own version:</p>
<pre><code>&lt;?php function custom_search_widget() { ?&gt;

&lt;form action="http://localhost/283/index.php" method="get"&gt;
	&lt;div&gt;
		&lt;label for="s"&gt;Site Search&lt;/label&gt;
		&lt;input id="s" name="s" alt="Search" type="text" /&gt;
	&lt;/div&gt;
&lt;/form&gt;

&lt;?php } if (function_exists('register_sidebar_widget'))

register_sidebar_widget(__('Search'), 'custom_search_widget'); ?&gt;</code></pre>
<h4>Hide unused widget areas</h4>
<p>An empty widgetized area of your theme can look strange, especially when <acronym title="Cascading Style Sheets">CSS</acronym> styles are applied to the container element. To prevent this from happening, we can wrap each of our widgetized areas with the following code:</p>
<pre><code>&lt;?php if (function_exists('is_sidebar_active') &amp;&amp; is_sidebar_active('sidebar')) { ?&gt;

&lt;!-- code from Step 2 or whatever you want --&gt;

&lt;?php } ?&gt;</code></pre>
<p>With this conditional code, widgetized areas will only be displayed if they contain at least one active widget. Nice&nbsp;:) Also note that this conditional technique works for displaying things other than widgets.</p>
<h3>More..?</h3>
<p>Hopefully this easy two-step widgetizing guide will be useful for you during your next round of theme development. As always, if you know of any other sweet widgetizing tricks, please share them in the comments!</p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/02/how-to-widgetize-wordpress-theme/">Permalink</a> | <a href="http://digwp.com/2010/02/how-to-widgetize-wordpress-theme/#comments">19 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/02/how-to-widgetize-wordpress-theme/&title=How to Widgetize Your WordPress Theme in 2&nbsp;Steps">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a>,  <a href="http://digwp.com/category/theme/" title="View all posts in Theme" rel="category tag">Theme</a> | Tagged: <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/theme/" rel="tag">Theme</a>, <a href="http://digwp.com/tag/widgets/" rel="tag">widgets</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/02/how-to-widgetize-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Remove Private/Protected from Post&#160;Titles</title>
		<link>http://digwp.com/2010/02/remove-private-or-protected-from-post-titles/</link>
		<comments>http://digwp.com/2010/02/remove-private-or-protected-from-post-titles/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 17:10:07 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1369</guid>
		<description><![CDATA[I had the situation come up where I need a password-protected post in WordPress. Of course that is super easy in WordPress, you can set up a password for it right in the &#8220;Publish&#8221; box before publishing. But by default, WordPress appends &#8220;Protected: &#8221; to the front of the post title, before and after the [...]]]></description>
			<content:encoded><![CDATA[<p>I had the situation come up where I need a password-protected post in WordPress. Of course that is super easy in WordPress, you can set up a password for it right in the &#8220;Publish&#8221; box before publishing. But by default, WordPress appends &#8220;Protected: &#8221; to the front of the post title, before and after the password has been entered. I didn&#8217;t like that, and thought that the password box was clue enough that the material was password protected.</p>
<p>I ran across <a href="http://wordpress.org/support/topic/257930#post-1139522">some simple code</a> in the official WordPress forums with a solution (thanks, t31os_!). </p>
<p><span id="more-1369"></span></p>
<pre><code>function the_title_trim($title) {

	$title = attribute_escape($title);

	$findthese = array(
		'#Protected:#',
		'#Private:#'
	);

	$replacewith = array(
		'', // What to replace "Protected:" with
		'' // What to replace "Private:" with
	);

	$title = preg_replace($findthese, $replacewith, $title);
	return $title;
}
add_filter('the_title', 'the_title_trim');</code></pre>
<p>I saw a plugin to do this too, but I think since this is rather theme-specific, functions.php code makes the most sense.</p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/02/remove-private-or-protected-from-post-titles/">Permalink</a> | <a href="http://digwp.com/2010/02/remove-private-or-protected-from-post-titles/#comments">6 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/02/remove-private-or-protected-from-post-titles/&title=Remove Private/Protected from Post&nbsp;Titles">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/02/remove-private-or-protected-from-post-titles/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>WordPress Tip: Remove nofollow Attributes from Post&#160;Content</title>
		<link>http://digwp.com/2010/02/remove-nofollow-attributes-from-post-content/</link>
		<comments>http://digwp.com/2010/02/remove-nofollow-attributes-from-post-content/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 07:17:59 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[posts]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1352</guid>
		<description><![CDATA[If you have posts that include the nofollow attribute on links, you may at some point decide to remove them. By default, WordPress doesn&#8217;t insert nofollow attributes in post content, but there are a variety of plugins that will insert nofollow into all links in post content. Or perhaps you have been manually adding nofollow [...]]]></description>
			<content:encoded><![CDATA[<p>If you have posts that include the <code>nofollow</code> attribute on links, you may at some point decide to remove them. By default, WordPress doesn&rsquo;t insert <code>nofollow</code> attributes in post content, but there are a variety of plugins that will insert <code>nofollow</code> into all links in post content. Or perhaps you have been manually adding <code>nofollow</code> tags to your post links for <acronym title="Search Engine Optimization">SEO</acronym> purposes. Regardless of how they got there, it&rsquo;s very easy to clean things up and remove all <code>nofollow</code> attributes from post content.</p>
<p><span id="more-1352"></span></p>
<h3>Remove <code>nofollow</code> attributes from post content</h3>
<p>To clean up your post content, simply add the following code to your active theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>function remove_nofollow($string) {
	$string = str_ireplace(' rel="nofollow"', '', $string);
	return $string;
}
add_filter('the_content', 'remove_nofollow');</code></pre>
<p>That&rsquo;s all there is to it &#8212; no further editing required. Once in place, this function will filter all post content and remove any instances of <code>rel="nofollow"</code>. Note that if you have <a href="http://perishablepress.com/press/2007/09/23/much-ado-about-nofollow-the-perishable-press-dofollow-series/" title="Much ado about nofollow: The Perishable Press Dofollow Series">some posts about nofollow</a>, this function will remove <code>rel="nofollow"</code> from your code examples as well.</p>
<h3>Remove <code>nofollow</code> attributes from post content and comment text</h3>
<p>It&rsquo;s just as easy to remove <code>nofollow</code> tags from comment text by including another <code>add_filter</code> function:</p>
<pre><code>function remove_nofollow($string) {
	$string = str_ireplace(' rel="nofollow"', '', $string);
	return $string;
}
add_filter('the_content', 'remove_nofollow');
add_filter('comment_text', 'remove_nofollow');</code></pre>
<p>With the additional <code>add_filter</code>, this code will eliminate all instances of <code>rel="nofollow"</code> that would have otherwise appeared in your post content and comment text.</p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/02/remove-nofollow-attributes-from-post-content/">Permalink</a> | <a href="http://digwp.com/2010/02/remove-nofollow-attributes-from-post-content/#comments">18 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/02/remove-nofollow-attributes-from-post-content/&title=WordPress Tip: Remove nofollow Attributes from Post&nbsp;Content">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/posts/" rel="tag">posts</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/02/remove-nofollow-attributes-from-post-content/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Leave Comments Open Forever on Specific Categories or Page&#160;Templates</title>
		<link>http://digwp.com/2010/01/leave-comments-open-forever-on-specific-categories-or-page-templates/</link>
		<comments>http://digwp.com/2010/01/leave-comments-open-forever-on-specific-categories-or-page-templates/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 20:50:18 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1229</guid>
		<description><![CDATA[I like the idea of shutting off comments after a certain number of days. Here on Digging Into WordPress we do it after 90. After that kind of time, the &#8220;community&#8221; of the discussion is long over. I think a good practice for turning off comments is to instead leave a message informing visitors that [...]]]></description>
			<content:encoded><![CDATA[<p>I like the idea of shutting off comments after a certain number of days. Here on Digging Into WordPress we do it after 90. After that kind of time, the &#8220;community&#8221; of the discussion is long over. I think a good practice for turning off comments is to instead leave a message informing visitors that the comment thread is closed, and offer a course of action in case they have something of grave importance to share.</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/blog-images/csstrickscommentsclosed-2.png" width="555" height="108" alt="" title="" /><br />
The &#8220;comments closed&#8221; message on CSS-Tricks
</div>
<p>Doing this is as simple as going to Settings > Discussion in your Admin area and setting the number of days before comments are turned off. This setting is <strong>completely global</strong> in that any single place on your entire site where comments are enabled, they will close after this number of days. On CSS-Tricks, I decided that while I like closing comments on articles after a certain amount of days, I wanted to leave comments open forever on my &#8220;snippets&#8221;, as I thought that those really aren&#8217;t meant for lengthy discussions anyway, and the comments are more for people sharing what works and what doesn&#8217;t. There really is no need for comments to be turned on for those. </p>
<p>Unfortunately I had no idea how to accomplish such a thing. So I emailed the smart guys over at <a href="http://wpengineer.com/">WP Engineer</a>, who came up with <a href="http://wpengineer.com/exclude-category-from-turnoff-comments-automatically/">some good stuff</a>.</p>
<p><span id="more-1229"></span></p>
<h3>Leave comments open forever on a particular CATEGORY</h3>
<p>This is the code in it&#8217;s original form from WP Engineer:</p>
<pre><code>/**
 * Close comments on an old post.  Hooked to comments_open and pings_open.
 * small modifications on the default functions of WordPress
 *
 * @param bool $open Comments open or closed
 * @param int $post_id Post ID
 * @param int $cat Category ID (cat_ID), set '' or empty for all categories
 * @param int $days_old Days to close comments, set '' for use value from settings
 * @return bool $open
 */
function wpe_close_comments_for_old_post( $open, $post_id, $cat = array(1, 4), $days_old = '' ) {
	if ( !$open )
		return $open;
 
	if ( in_category($cat) )
		return $open;
 
	if ( '' === $days_old )
		$days_old = (int) get_option('close_comments_days_old');
 
	if ( !$days_old )
		return $open;
 
	$post = get_post($post_id);
 
	if ( time() - strtotime( $post-&gt;post_date_gmt ) &gt; ( $days_old * 24 * 60 * 60 ) )
		return false;
 
	return $open;
}
add_filter( 'comments_open', 'wpe_close_comments_for_old_post', 10, 2 );
add_filter( 'pings_open',    'wpe_close_comments_for_old_post', 10, 2 );</code></pre>
<h3>Leave comments open forever for a particular PAGE TEMPLATE</h3>
<p>My own needs were slightly different in that I needed this to work for a special page template not a specific category (my snippets use page/parent structure not posts). I also just wanted regular posts to default to whatever setting was in the Admin, not set that number from the function. </p>
<p>And so my modified version is:</p>
<pre><code>function wpe_close_comments_for_old_post( $open, $post_id, $template = 'page-snippet.php' ) {

	global $wp_query;
	
	if ( !$post_id ) {
		$post_id = $wp_query-&gt;post-&gt;ID;
    }
 
	if ( get_post_meta($post_id, '_wp_page_template', true) == $template &amp;&amp; get_option('close_comments_for_old_posts') ) {
		return true;
	}

	return $open;
	
}
add_filter( 'comments_open', 'wpe_close_comments_for_old_post', 10, 2 );
add_filter( 'pings_open',    'wpe_close_comments_for_old_post', 10, 2 );</code></pre>
<p>Works like a charm!</p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/01/leave-comments-open-forever-on-specific-categories-or-page-templates/">Permalink</a> | <a href="http://digwp.com/2010/01/leave-comments-open-forever-on-specific-categories-or-page-templates/#comments">2 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/01/leave-comments-open-forever-on-specific-categories-or-page-templates/&title=Leave Comments Open Forever on Specific Categories or Page&nbsp;Templates">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/comments/" rel="tag">comments</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/01/leave-comments-open-forever-on-specific-categories-or-page-templates/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Custom Query Shortcode: Run a Loop inside Any&#160;Post/Page</title>
		<link>http://digwp.com/2010/01/custom-query-shortcode/</link>
		<comments>http://digwp.com/2010/01/custom-query-shortcode/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 14:43:59 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[shortcode]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1283</guid>
		<description><![CDATA[I had the occasion yesterday to have a page with a section on it where it would output a very specific set of other pages, which would need to change dynamically. What I could have done is built a special page template for this page, and inside that template run a query_posts() to get these [...]]]></description>
			<content:encoded><![CDATA[<p>I had the occasion yesterday to have a page with a section on it where it would output a very specific set of other pages, which would need to change dynamically. What I could have done is built a special page template for this page, and inside that template run a <code>query_posts()</code> to get these posts. But I wanted this page to remain editable from the admin. Besides, creating a special page template every time you need to do something like this is too cumbersome. WordPress is extensible enough to do better. </p>
<p>Hence, the Custom Query Shortcode!</p>
<p><span id="more-1283"></span></p>
<p>This is basically how I wanted it to work. You have a shortcode with one single parameter called &#8220;query&#8221;. (Remember, shortcodes are those things in brackets you can use in the body of Pages/Posts that do special stuff). In the &#8220;query&#8221; parameter, you literally enter exactly what you would enter for a query when using the <code>query_posts()</code> function. In other words:</p>
<pre><code>[loop query=""]</code></pre>
<p>You can pass whatever query you want to it. For example, let&#8217;s say you wanted to query for 20 Pages that were the child of a specific page and list them in Ascending order:</p>
<pre><code>[loop the_query="showposts=20&amp;post_type=page&amp;post_parent=453&amp;ord=ASC"]</code></pre>
<h3>What it returns</h3>
<p>This is totally customizable in the function itself, but I made the judgement call for myself that it would return list items with linked post titles inside. So you&#8217;d go like this:</p>
<pre><code>&lt;ul&gt;
[loop query="posts_per_page=3"]
&lt;/ul&gt;</code></pre>
<p>and you&#8217;d get:</p>
<pre><code>&lt;ul&gt;
&lt;li&gt;&lt;a href="http://digwp.com/post-1/"&gt;Post 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://digwp.com/post-2/"&gt;Post 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://digwp.com/post-3/"&gt;Post 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>And of course those would be real URL&#8217;s and real Post titles&#8230;</p>
<h3>The Code</h3>
<p>This would go in your themes functions.php file. Now that I&#8217;m thinking more clearly about it, this probably makes more sense to be a plugin since it probably shouldn&#8217;t be theme-dependant, but oh well, we can do that another day.</p>
<pre><code>function custom_query_shortcode($atts) {

   // EXAMPLE USAGE:
   // [loop the_query="showposts=100&amp;post_type=page&amp;post_parent=453"]
   
   // Defaults
   extract(shortcode_atts(array(
      "the_query" =&gt; ''
   ), $atts));

   // de-funkify query
   $the_query = preg_replace('~&amp;#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
   $the_query = preg_replace('~&amp;#0*([0-9]+);~e', 'chr(\\1)', $the_query);

   // query is made               
   query_posts($the_query);
   
   // Reset and setup variables
   $output = '';
   $temp_title = '';
   $temp_link = '';
   
   // the loop
   if (have_posts()) : while (have_posts()) : the_post();
   
      $temp_title = get_the_title($post-&gt;ID);
      $temp_link = get_permalink($post-&gt;ID);
      
      // output all findings - CUSTOMIZE TO YOUR LIKING
      $output .= "&lt;li&gt;&lt;a href='$temp_link'&gt;$temp_title&lt;/a&gt;&lt;/li&gt;";
          
   endwhile; else:
   
      $output .= "nothing found.";
      
   endif;
   
   wp_reset_query();
   return $output;
   
}
add_shortcode("loop", "custom_query_shortcode");</code></pre>
<h3>Credit</h3>
<p>When I first wrote this up to use, it totally didn&#8217;t work. I couldn&#8217;t figure it out&#8230; the query was getting passed over just fine, and if I hard coded the query, the whole thing would work just fine. It turns out that because the shortcode lived inside the content of a Post/Page, the ampersand characters in the query were getting encoded, and thus screwing up the query. </p>
<p><a href="http://sketchpad.co.uk/">Dan Smith</a> had the genius idea to use html_entity_decode() on the query before using it in query_posts(), but for some reason I couldn&#8217;t get it to take. </p>
<p><a href="http://www.rogerstringer.com/">Roger Stringer</a> coded up the solution with regular expressions you see above. Thanks guys!</p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/01/custom-query-shortcode/">Permalink</a> | <a href="http://digwp.com/2010/01/custom-query-shortcode/#comments">8 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/01/custom-query-shortcode/&title=Custom Query Shortcode: Run a Loop inside Any&nbsp;Post/Page">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/loop/" rel="tag">loop</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/01/custom-query-shortcode/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Google Maps&#160;Shortcode</title>
		<link>http://digwp.com/2010/01/google-maps-shortcode/</link>
		<comments>http://digwp.com/2010/01/google-maps-shortcode/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 01:34:59 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[shortcode]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1224</guid>
		<description><![CDATA[David Hollander of SparkWeb Interactive sent us in a little code clip for inserting Google Maps into Posts/Pages by the use of shortcodes. Google actually has copy-and-pastable iframe code already in Google Maps that is really easy to snag, but David was having problems with the Visual text editor screwing up the code when saving [...]]]></description>
			<content:encoded><![CDATA[<p>David Hollander of <a href="http://sparkweb.net/">SparkWeb Interactive</a> sent us in a little code clip for inserting Google Maps into Posts/Pages by the use of shortcodes. Google actually has copy-and-pastable iframe code already in Google Maps that is really easy to snag, but David was having problems with the Visual text editor screwing up the code when saving the Post. With a short code, no problem.  </p>
<p><span id="more-1224"></span></p>
<h3>Put in functions.php file</h3>
<pre><code>//Google Maps Shortcode
function fn_googleMaps($atts, $content = null) {
   extract(shortcode_atts(array(
      "width" =&gt; '640',
      "height" =&gt; '480',
      "src" =&gt; ''
   ), $atts));
   return '&lt;iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'"&gt;&lt;/iframe&gt;';
}
add_shortcode("googlemap", "fn_googleMaps");</code></pre>
<p>Usage inside Post/Page:</p>
<pre><code>[googlemap width="200" height="200" src="[url]"]</code></pre>
<p>Notice the three parameters above. Width and height are self explanitory, don&#8217;t use them at all for the default values set in the function itself. The src URL you can snag from Google Maps like this:</p>
<p><img src="http://digwp.com/wp-content/blog-images/mapsURL.png" width="590" height="294" alt="" title="" /></p>
<h3>Easy!</h3>
<p>This post is a good reminder of how easy it is to build your own shortcodes for specific bits of functionality that are specifically useful to you, whatever that might be. In the past, we&#8217;ve covered making <a href="http://digwp.com/2009/09/easy-shortcode-permalinks/">shortcode permalinks</a>. </p>
<p style="border:1px solid #ccc; background: #eee; line-height: 20px; padding: 5px 10px; margin-top: 10px;">Like the article? <a href="http://digwp.com/book"><strong>Get the book!</strong></a></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/01/google-maps-shortcode/">Permalink</a> | <a href="http://digwp.com/2010/01/google-maps-shortcode/#comments">11 comments</a> | Add to
<a href="http://del.icio.us/post?url=http://digwp.com/2010/01/google-maps-shortcode/&title=Google Maps&nbsp;Shortcode">Delicious</a><br />
Categorized: <a href="http://digwp.com/category/php/" title="View all posts in PHP" rel="category tag">PHP</a> | Tagged: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/01/google-maps-shortcode/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
