Take the 2-minute tour ×
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

1 Answer 1

up vote 6 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

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.