Proper use of Output Buffer

I’m attempting to use actions to override function calls I currently have throughout a template (to make updating certain replicated sections easier). For example, in archives.php I have the following:

<?php get_header(); ?>

    <?php roots_content_before(); ?>
    <?php $page_for_posts = get_option( 'page_for_posts' ); if ($page_for_posts) { echo '<h1>' . get_the_title($page_for_posts) . '</h1>'; } ?>
            $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
            if ($term) {
                echo $term->name;
            } elseif (is_day()) {
                printf(__('Daily Archives: %s', 'roots'), get_the_date());
            } elseif (is_month()) {
                printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
            } elseif (is_year()) {
                printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
            } elseif (is_author()) {
                global $post;
                $author_id = $post->post_author;
                printf(__('Author Archives: %s', 'roots'), get_the_author_meta('user_nicename', $author_id));
            } else {
    <?php echo category_description(); ?>
    <?php roots_loop_before(); ?>
    <?php get_template_part('loop', 'category'); ?>
    <?php roots_loop_after(); ?>
    <?php roots_content_after(); ?>

<?php get_footer(); ?>

You can see a few of the functions, like roots_content_before(); In a separate file, I have the following:

function roots_content_before() { do_action('roots_content_before'); }

and use it as follows:


    add_action('roots_content_before', 'roots_bootstrap_content_before');

    function roots_bootstrap_content_before() { ?>

        this is some text

    <?php }


From what I’ve read, especially if I’m going to have large chunks of code, I should be using the output buffer, but when I try to do this, I’m getting diddly squat:


    add_action('roots_content_before', 'roots_bootstrap_content_before');

    function roots_bootstrap_content_before() { ob_start(); ?> 

        this is some text

       <?php return ob_get_clean();



Am I thinking of this completely wrong? I am still learning, but have been trying for a bit without any success. Any pointers in the right direction would really be appreciated. Thanks!

Solutions Collecting From Web of "Proper use of Output Buffer"

No, you don’t need output buffering in this case. As a rule of thumb: Don’t use output buffering unless you really have to.

Just imagine what happens if someone else uses output buffering too from a plugin and it crosses with yours:

// plugin

// later, you in your theme

// you call a function where the plugin author hooked in to call:
print ob_get_clean();

// you call *your*:
return ob_get_clean();

// is is empty!

This is really hard to debug. Avoid it.

You don’t need a separate function to cover a plain do_action(). Just write do_action('roots_content_before'); in your theme.

@toscho’s answer is totally wrong.

  • Output buffering can be nested, there’s no need to worry about other Plugins.

  • In this post from 2009 there’s a very elegant way to fetch and manipulate the final output of WordPress.