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!

2 Answers 2


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.

  • 2
    Agree with toscho here. Don't use Output Buffering. Use action and filter calls where needed.
    – Scott
    Feb 2, 2012 at 18:11
  • Hi @toscho I really appreciate the input. I had only started to read about output buffering, but from that article, it sounded like there was a performance increase. Whether there actually is or not, it almost sounds like if you are controlling the entire application then it would be good, but since WordPress has thousands of plugins, you just might be asking for trouble ;) Thanks again!
    – Zach
    Feb 2, 2012 at 19:19
  • 4
    @Zach This article is wrong from the first line. :) Send your output to the user as soon as possible. In themes with long working code I even call flush() before and after the slow code to speed the rendering up.
    – fuxia
    Feb 2, 2012 at 20:05
  • The bastards! ;) Appreciate the further insight into that @toscho
    – Zach
    Feb 3, 2012 at 20:29
  • 1
    Progressive rendering via multiple flushes is a good read.
    – fuxia
    Feb 3, 2012 at 20:32

@toscho's answer is totally wrong.

  • 1
    This is a bit harsh. I think what @toscho tried to say isn't that you can't nest, but that if for some reason you call ob_get_clean() before the code of e.g. another plugin which started the first ob_start() calls ob_get_clean() you'll get unexpected results. He just warns that you should really only use it if there is no other way as there are drawbacks which you might not notice at first.
    – kraftner
    Mar 12, 2015 at 16:43
  • Did you look at the second link? There's a nice handling that addresses exactly the problem of this question. There's nothing unexpected when nesting; try it yourself: ob_start(); echo "0"; ob_start(); echo "a"; echo ob_get_clean(); echo "1"; $a = ob_get_clean(); echo $a;
    – Robert
    Mar 12, 2015 at 17:03
  • 2
    To be honest - no I didn't. Again all I am saying is that there are a lot of ways to f*** this up, so better only use it when there is no other way. On a sidenote: This is the exact reason why link-only answers are frowned upon here. If you had added the gist of your links to the answer that would have helped a lot.
    – kraftner
    Mar 13, 2015 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.