Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
Agree with toscho here. Don't use Output Buffering. Use action and filter calls where needed. – Brady Feb 2 '12 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 '12 at 19:19
@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. – toscho Feb 2 '12 at 20:05
The bastards! ;) Appreciate the further insight into that @toscho – Zach Feb 3 '12 at 20:29
Progressive rendering via multiple flushes is a good read. – toscho Feb 3 '12 at 20:32

@toscho's answer is totally wrong.

share|improve this answer
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 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 at 17:03
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 at 9:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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