[wp-trac] [WordPress Trac] #43258: Output buffering

WordPress Trac noreply at wordpress.org
Thu Feb 8 08:01:54 UTC 2018


#43258: Output buffering
------------------------------------+-----------------------------
 Reporter:  nextendweb              |      Owner:
     Type:  enhancement             |     Status:  new
 Priority:  normal                  |  Milestone:  Awaiting Review
Component:  General                 |    Version:
 Severity:  normal                  |   Keywords:
  Focuses:  docs, coding-standards  |
------------------------------------+-----------------------------
 I see that more and more theme and plugin developers start to use output
 buffering functions for the whole site as they need to manipulate the
 site's content. For example:
 * Cache the page
 * Combine JS and CSS files
 * Lad JS and CSS files for widgets only when needed
 * Place SEO related things

 As it is not officially available in WordPress, developers need to find
 their way to buffer the output. Probably the most common action is the
 'template_redirect', where they can place ob_start()

 Then they have to close their output buffer, probably the best action to
 do that is 'shutdown'.

 It wouldn't be a problem, if this method only used once on your site. When
 multiple plugin or theme use this technique, they should close only their
 output buffers. As output buffers are LIFO stacked, it is very important
 to close in the order they were added.

 For example:

 '''Cache plugin:'''
 {{{#!php
 <?php
 add_action('template_redirect', function(){
    ob_start();
 });

 add_action('shutdown', function(){
   $html = ob_get_clean();
   //Let's cache the html and show it...
 });
 }}}


 '''CSS minify plugin:'''
 {{{#!php
 <?php
 add_action('template_redirect', function(){
    ob_start();
 });

 add_action('shutdown', function(){
   $html = ob_get_clean();
   //Let's find CSS files, minify them and replace the originals
 });
 }}}



 In this case the page will be cached and the CSS files will be minified
 afterwards which will slow down the site as they should be in reverse
 order. We can fix that with priority, but both 'template_redirect' and
 'shutdown' should get the same priority to make sure we close the related
 output buffer.

 '''Documentation'''
 What I propose is to have an official documentation which suggests the
 right way to use output buffering. It would help prevent several conflicts
 between plugins and themes.

 '''Future'''
 It would be great to see in WordPress core an in-built output buffering
 system. Then the developers wouldn't need to start and close output
 buffers on their own. WordPress would do the output buffering and at the
 end it would allow the filtering of the content.

 {{{#!php
 <?php
 echo apply_filters('wp_output', $output);
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43258>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list