Like the blog? Get the book »

Putting the_content() into a PHP Variable

Putting the_content() into a PHP Variable

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

$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.


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 my site 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));


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. Why not just use get_the_content?

  2. Jason Lengstorf

    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. 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.

    • 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. 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 for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace