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.
Read the rest of this entry »