Dirty Ten, a Twenty Ten Child Theme using Output Buffers

After making Thirty Ten, my first Twenty Ten Child Theme, Devin Price asked for an example of a child theme that used output buffers.I’m not a big fan of using output buffers in my themes, but it’s a worthwhile trick to have in your bag. There are two main reasons for this:

  1. It makes code harder to read.
  2. It makes it so html isn’t being sent immediately to the browser, which often means your loading time increases

That being said, you shouldn’t be afraid to use them. Just make sure you are documenting your code (which you should be doing anyway), and that you are using some sort of caching so you don’t have to regenerate the page on every load (also something you should be doing anyway).

I’m going to do a quick example and show you how to make a twenty-ten child theme that adds a widget area above the image and below the Site title and Description. To do this we are going to start our buffer at wp-head and then use the filters used for the header image as an action to end it and add our widget.

&lt;br&gt;&lt;br&gt;add_action(&#8216;wp_head&#8217;, &#8216;head_buffer&#8217;);<br />
/**<br />
* Start our buffer and Use filters as actions and add them to end our buffer depending on the header image<br />
*<br />
*/<br />
function head_buffer(){<br />
ob_start();<br />
if ( is_singular() &amp;&amp; has_post_thumbnail( $post-&amp;gt;ID ) &amp;&amp; $width &gt; HEADER_IMAGE_WIDTH ) :<br />
	add_filter(&#8216;post_thumbnail_html&#8217;, &#8216;clear_buffer_post_thumbnail&#8217;);<br />
else :<br />
	 add_filter(&#8216;theme_mod_header_image&#8217;, &#8216;clear_buffer_header_image&#8217;);<br />
endif;<br />
<p>/**<br />
* Our filter is really an action<br />
*<br />
*/<br />
function clear_buffer_header_image($html){<br />
	clear_buffer();<br />
	return $html;<br />
<p>/**<br />
* The Buffer ender. Clear it, add to it, echo it.<br />
*<br />
* Replace the blank space after our site description div with our header widget.  This is called by both<br />
* of our action psuedo filters<br />
*/<br />
function clear_buffer(){<br />
	$buffer = ob_get_clean();<br />
	$header = 	preg_replace(&#8216;#&amp;lt;div id=&quot;site-description&quot;&amp;gt;.*&amp;lt;/div&amp;gt;#&#8217;,&quot;$0&quot;.new_header_widget() ,$buffer);<br />
	echo $header;<br />
<p>function clear_buffer_post_thumbnail($html){<br />
	clear_buffer();<br />
	return $html;<br />
}<br />

Our next step is to register our widget and add a function that will add the content of our widget.

<br />
/**<br />
* This adds the content of our widget.  Could we use a new sidebar-*.php file?  yes, but it&#8217;s easier to keep all of this in one file<br />
*<br />
<p>function new_header_widget(){<br />
	ob_start();<br />
?&gt;<br />
		&lt;div id=&quot;headerwidget&quot; class=&quot;widget-area&quot;&gt;<br />
	 	&lt;ul class=&quot;xoxo&quot;&gt;<br />
&lt;?php if ( ! dynamic_sidebar( &#8216;header-widget-area&#8217; ) ) : // begin Header widget area ?&gt;<br />
		&lt;li id=&quot;search&quot; class=&quot;widget-container widget_search&quot;&gt;<br />
			&lt;?php get_search_form(); ?&amp;&gt;<br />
		&lt;/li&gt;<br />
		&lt;li id=&quot;archives&quot; class=&quot;widget-container&quot;&gt;<br />
				&lt;h3 class=&quot;widget-title&quot;&amp;&gt;&lt;?php _e( &#8216;The Maker&#8217;, &#8216;dirtyten&#8217; ); &gt;&lt;/h3&gt;<br />
				&lt;/ul&gt;<br />
					&amp;lt;li&amp;gt;&amp;lt;a href=&#8217;https://aaron.jorb.in&#8217;&amp;gt;Wordpress Developer Aaron Jorbin&amp;lt;/a&amp;gt; creates themes and plugins for both the masses and the individual&amp;lt;/li&amp;gt;&lt;br&gt;					&amp;lt;li&amp;gt;&amp;lt;a href=&#8217;https://aaron.jorb.in/thirtyten&#8217;&amp;gt;Thirty Ten&amp;lt;/a&amp;gt; is a Twenty Ten Child theme designed to teach you how to make child themes&amp;lt;/li&amp;gt;&lt;br&gt;				&amp;lt;/ul&amp;gt;&lt;br&gt;			&amp;lt;/li&amp;gt;&lt;br&gt;&lt;br&gt;			&amp;lt;li id=&quot;meta&quot; class=&quot;widget-container&quot;&amp;gt;&lt;br&gt;				&amp;lt;h3 class=&quot;widget-title&quot;&amp;gt;&amp;lt;?php _e( &#8216;Meta&#8217;, &#8216;twentyten&#8217; ); ?&amp;gt;&amp;lt;/h3&amp;gt;&lt;br&gt;				&amp;lt;ul&amp;gt;&lt;br&gt;					&amp;lt;?php wp_register(); ?&amp;gt;&lt;br&gt;					&amp;lt;li&amp;gt;&amp;lt;?php wp_loginout(); ?&amp;gt;&amp;lt;/li&amp;gt;&lt;br&gt;					&amp;lt;?php wp_meta(); ?&amp;gt;&lt;br&gt;				&amp;lt;/ul&amp;gt;&lt;br&gt;			&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;?php endif; // end Header widget area ?&amp;gt;&lt;br&gt;			&amp;lt;/ul&amp;gt;&lt;br&gt;		&amp;lt;/div&amp;gt;&amp;lt;!&#8211; #headerwidget .widget-area &#8211;&amp;gt;&lt;br&gt;&amp;lt;?&lt;br&gt;	$widget_area = ob_get_clean();&lt;br&gt;	return $widget_area;&lt;br&gt;}&lt;br&gt;&lt;br&gt;add_action( &#8216;init&#8217;, &#8216;dirtyten_widgets_init&#8217; );&lt;br&gt;&lt;br&gt;/**&lt;br&gt;* Register our widget&lt;br&gt;*&lt;br&gt;*/&lt;br&gt;function dirtyten_widgets_init() {&lt;br&gt;	// Area 1&lt;br&gt;	register_sidebar( array (&lt;br&gt;		&#8216;name&#8217; =&amp;gt; &#8216;Header Widget Area&#8217;,&lt;br&gt;		&#8216;id&#8217; =&amp;gt; &#8216;header-widget-area&#8217;,&lt;br&gt;		&#8216;description&#8217; =&amp;gt; __( &#8216;The widget area in the Header.  Optimized to use three widgets&#8217; , &#8216;dirtyten&#8217; ),&lt;br&gt;		&#8216;before_widget&#8217; =&amp;gt; &#8216;&amp;lt;li id=&quot;%1$s&quot; class=&quot;widget-container %2$s&quot;&amp;gt;&#8217;,&lt;br&gt;		&#8216;after_widget&#8217; =&amp;gt; &quot;&amp;lt;/li&amp;gt;&quot;,&lt;br&gt;		&#8216;before_title&#8217; =&amp;gt; &#8216;&amp;lt;h3 class=&quot;widget-title&quot;&amp;gt;&#8217;,&lt;br&gt;		&#8216;after_title&#8217; =&amp;gt; &#8216;&amp;lt;/h3&amp;gt;&#8217;,&lt;br&gt;	) );&lt;br&gt;&lt;br&gt;}&lt;br&gt;&lt;br&gt;

And that’s about it. It’s 111 lines after comments and white space. I’d strongly encourage you to add your own widgets, unless you want to be advertising for me ;).

The only other part is our css to style the widgets. This is also simple:

&lt;br&gt;@import url('../twentyten/style.css');&lt;br&gt;&lt;br&gt;#headerwidget{&lt;br&gt;clear:both;&lt;br&gt;width: 100%;&lt;br&gt;}&lt;br&gt;&lt;br&gt;#headerwidget ul li{&lt;br&gt;float: left;&lt;br&gt;width: 280px;&lt;br&gt;margin-right: 30px;&lt;br&gt;&lt;br&gt;}&lt;br&gt;

You can check out Dirty Ten, or Download it and use it.  You can also clone it from the mercurial repository.



Leave a Reply

Your email address will not be published. Required fields are marked *