[wp-hackers] Replace default doctype on one page only through plugin

Lynne Pope lynne.pope at gmail.com
Mon Jan 19 12:36:46 GMT 2009

Output buffering can be really tricky. ob_start starts a buffering process
that prevents anything being output to the browser until the output is sent
to the browser at the end of the request, or ob_end_flush (or ob_get_clean)
are used. Right? Well, if you are writing your own script that is not being
used with anyone else's work then its supposedly easy enough to control the
sequence in which code is being run (I say "supposedly" because I have
wasted hours with ob_start and these days prefer to just avoid it where
The big problems come in when things get out of sequence, or where code is
being processed differently to the way you expect. If you buffer output and
some other code closes a buffer, depending on the sequence within the stack,
you can find your output turning up in strange places. Or not showing up at

So, if I was to run a plugin, say, that uses ob_start and another plugin
also uses it, plugin 2 could end up closing the buffer on plugin 1, or
outputting content from the wrong buffer, depending on the sequence in which
PHP processes it. The risk of things going wrong increases with every
instance of ob_start.  Because PHP closes all open buffering and outputs it
when the script executes, without the need for ob_end_flush or anything to
explicitly close the buffer, working out where things are going squiffy can
be a real nightmare.

This is simplified and I probably haven't explained it very well, but it
might give you an idea why its a bad idea to use it in plugins for
WordPress, unless you are prepared to have users complaining about
incompatibility with other plugins.


2009/1/19 Mike Schinkel <mikeschinkel at newclarity.net>

> Gaarai <gaarai at gaarai.com> wrote:
> > The problem is that the ob_start function does not nest. There is no
> > way to guarantee that nothing else will call ob_start between the
> > start_output_collection() and send_output() functions. On a standard
> > install with the default theme, there are a total of 45 apply_filters
> > calls as well as requiring the theme's header.php code. At any point
> > in executing an of these sections of code, some code may exist that
> > makes use of ob_start.
> I'm confused.  According to PHP.NET[1]:
>   Output buffers are stackable, that is, you may call ob_start()
>   while another ob_start() is active. Just make sure that you call
>   ob_end_flush() the appropriate number of times. If multiple output
>   callback functions are active, output is being filtered sequentially
>   through each of them in nesting order.
> And this[2] explicitly says you can nest calls. Obviously, I'm missing
> something as your statement and PHP.NET seem to contradict. I'm sure it's
> my misunderstanding; I've been coding for 20 years but only PHP for two and
> only recently on a hard-core basis so I haven't learned all it's
> ins-and-outs of PHP. So I'm hoping you can help me better understand this
> apparent dichotomy.
> TIA.
> -Mike Schinkel
> http://mikeschinkel.com/custom-wordpress-plugins/
> [1] http://us3.php.net/ob_start
> [2] http://www.codewalkers.com/c/a/Miscellaneous/PHP-Output-Buffering/1/
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list