DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Putting the_content() into a PHP Variable

Posted by on

There are probably a couple ways to do this, but here is a really easy one:

ob_start();
the_content();
$content = ob_get_clean();


This is called “output buffering” where the output is redirected from being directly sent out to being written to a buffer.

Why?

In case you need to do any manipulation or calculation of the content before outputting it. Admittedly, it’s probably not a very common need.

For example, on a recent redesign of one of my sites Quotes on Design, I wanted to count the number of words in a quote so I could set the font-size based on that.

$numWords =  sizeof(explode(" ", $content));

Warning

There are things you could do with this that are probably best done in other ways. For example, maybe you want to remove the paragraph tags that WordPress likes to append to content automatically for you. You’d be better off removing that filter (from your functions.php file) than messing with this.

remove_filter ('the_content','wpautop');

17 Responses

  1. Hiranthi July 2, 2009

    Why not just use get_the_content?

  2. A far easier way to do this is to just use the built-in WP function get_the_content(), which will return a string that you can store in a variable:

    $content = get_the_content();

    Output buffering is a good trick, but it’s definitely not necessary in this case. :)

  3. Or you can just do:

    $content = get_the_content();

  4. I prefer to use the (built in) function

    $content = get_the_content()

    it just seems simpler to me :)

  5. There’s a built-in way to do so: get_the_content()

    Check the docs before writing. :)

  6. Rarst July 3, 2009

    Saw snippet based on this at WpRecipes. :)

    Is much different from simply using $post->post_content ? Had looked through xref but it lost me somewhere around get_the_content()

  7. You can also use get_the_content() which allows the content to be stored in a variable.

  8. Enough already with the “get_the_content()” jabber. We get it wordpress has a function set up to pull in the content without using a buffer. WordPress uses get_ for returning almost any function inside the loop without echoing out to the browser. comes in handy for conditional statements.

    • Rarst July 4, 2009

      Sorry, not our fault. Everyone made comment without seeing rest because they were all stuck in moderation. Then bulk approval and we all look bit stupid. :)

      • wasn’t trying to sound upset or mean I just got tired of reading the same comments over and over. I am not trying to stop anyone from commenting because that is what makes blogs like this informative. I just hate when people read a post and then drop right to the bottom (without reading any other comments) and start bashing a quality post apart.

  9. I might be wrong but after following Chris for the last few years I would think that he knows about the “get_the_content()” function and has some weird reasoning behind doing it another way. I am waiting to hear his response to this. So Chris when you read this please do me a favor and let me know one way or the other.

  10. Honestly I didn’t know about it! =)

    I thank everyone for pointing it out. Obviously, that is far easier.

    And yes, I’ve been in the middle of moving and most of these comments were stuck in moderation, sorry about that!

  11. That last bit of code for removing the p-tags would come in handy but where does it go? In the wordpress loop or in the functions.php file or…?

  12. Note that this will not work if output buffering is already in use on the site.

    Also, I’m sure there’s a function that returns the number of words in a string… No need to use that explode method. And if there’s no function for it, using substr_count should be faster than count(explode()) :P

  13. Angelus July 31, 2009

    get_the_content , don’t have all content , end of line isn’t formatted in that function

  14. Hi!

    I figured out another way of doing this. Look at my post “get_the_content WITH formatting”.

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry