• I’ve seen a few people talking about this issue but never found an answer.

    Is there a way (preferably an easy one) to have WP functions like get_calendar(), get_links(), etc… return to a variable instead of echo?

    This seems like something that would be quite handy and used a lot by people integrating WP into an existing site.

    Can anyone answer this or at least explain why these functions echo inside the class instead of just using <? echo get_calendar(); ?> in the template?

    Functionally, this would work the same as the default <? get_calendar(); ?>, so why not have a simple way to do it?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Use :

    <?php

    ob_start();
    the_tag_here();
    $my_stuff = ob_get_contents();
    ob_end_clean();

    print($my_stuff); //or whatever else

    ?>

    Alex King was the first person I heard talking about something like this, but it works, and uses normal php functions. The idea is to buffer the output and then use that.

    Thread Starter jcornelius

    (@jcornelius)

    Output buffering seems like a sloppy way to go about this though. I agree that it will work, but is there not a better way?

    This introduces several other issues as well. You can’t pass the output of functions to other objects, or assign them to templates systems like Smarty as easily.

    Surely someone out there has a good solution. If not I guess I’ll have to hack all the functions myself and post something for the next guy that needs it.

    There had been some discussion about including variations of the functions that return stuff instead of echoing them, at the hackers mailing list.

    I suppose no one has had the time to modify the files and submit a diff. I will look into it as soon as I have an afternoon to spare.

    Surely someone out there has a good solution. If not I guess I’ll have to hack all the functions myself and post something for the next guy that needs it.

    That’s the Open Source way! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘echo vs. return for WP functions’ is closed to new replies.