New here? Read Greetings Earthling!

Output Buffering | Complex Inline HTML | Wordpress Plugin Writers

I wanted a simple method to handle some output buffering when coding plugins. Let's take this situation. You want to alter the_content filter but you want to make the modifications inline.

To manage inline HTML in PHP you got two options: regular string ... impossible. HEREDOCs ... not good for complex PHP code. So you are left with regular ?>HTML + PHP<?php. But returning this as a variable is not trivial. You need to output buffer it by using ob_start(); ... HTML + PHP code ... $html = ob_get_contents(); ob_end_clean();. And this will allow you to wrap inside a variable any kind of complex HTML code thanks to super output buffering in PHP.

Why I need this?

I need this trick to alter the_content filter with inline HTML but wanted a more simple version. And this is what I got for you today. Instead of the regular code that would look like this:

<?php // ---------------------------------------------------------------- function my_the_content_filter($content){ ob_start(); ?> <!-- combine HTML + PHP here to produce any kind of output --> <?php $ob = ob_get_contents(); ob_end_clean(); return $content.$ob; } add_filter('the_content','my_the_content_filter',11); // ---------------------------------------------------------------- ?>

... you could use this simple version I made:

<?php // ---------------------------------------------------------------- function my_the_content_filter($content){ $obWrapper = new elObWrap(); // Instantiate the class ?> <!-- combine HTML + PHP here to produce any kind of output --> <?php $ob = (string)$obWrapper; // ... cast is __toString // On __destruct output buffering is disabled // You can force this using unset($obWrapper); return $content.$ob; // Append it } add_filter('the_content','my_the_content_filter',11); // ---------------------------------------------------------------- ?>

This is not a really big difference thing but, IMHO, it's easier to use like this than the ob_* functions. Have fun with it and expand it as you wish.

The elOBWrap Class

Zone unavailable to unregistered users.
Registration is FREE, quick, painless and worth its weight in gold.

If you can't figure this out or what it does then you probably don't need it. But if you write Wordpress plugins or need complex code wrapped into variables for further use this is what you need. This code needs PHP5+ and use the magic methods. Click the link to learn more about them. The __toString is true magic and works when classes are cast to string.

Category: PHP, Wordpress Plugins
Tagged: , ,

3 Responses

  1. What are you using this for specifically?

    I am curious because a plugin that I wrote uses the “the_content” filter for “continuous update” (no post insertion; continuously updated on access) vs. the “content_save_pre” filter for “static” update (actual post insertion; none updateable).

    I personally would like the best of both worlds and you might be further along than I am on this topic.

    Communibus

    • $@5ubliminal52:357 — #1 says:

      It makes wrapping advanced PHP + HTML output to a variable for further, use a breeze.
      It’s fit for the moments when HEREDOCs and strings don’t help.

      Let’s say you need to wrap this code to a string.
      for($i=0;$i<10;$i++){ echo number_format($i); }
      By wrapping it in ob functions you capture the output. If you don’t it outputs directly. Capturing the output allows you to append advanced HTML to other variables.

      I can’t explain better than this. It’s too simple.

  2. Thanks a lot for sharing.

Leave a Reply

Comment Links are DoFollow only for registered subscribers … if comments pass moderation. Links in your comments may be slaughtered, hijacked or removed.