get_the_content() WITH formatting

August 31st, 2008

Normally the get_the_content() tag returns the content without formatting. I found out a solution to make get_the_content() tag return the same content as the_content().

<?php echo get_the_content(); ?>
<?php the_content(); ?>

For me and others that can be a problem if you expect them to behave the same. I looked into the WordPress core and found a solution. Put this code into your functions.php in your theme folder.

function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
	$content = get_the_content($more_link_text, $stripteaser, $more_file);
	$content = apply_filters('the_content', $content);
	$content = str_replace(']]>', ']]&gt;', $content);
	return $content;

In your theme, call the function within the loop:

<?php get_the_content_with_formatting(); ?>

To get more new functions/template tags, install my plugin WP Extra Template Tags.

  1. 2012-04-02 - Themenmixer

    That was what I was looking for. Used your solution to switch some content on my blog. Thx 4 sharing.

  2. 2012-03-27 - Krzysztof

    Thank you so much. It works great on my site.

  3. 2012-01-31 - Glenford Nunez

    Awesome! Thanks for this!

  4. 2011-11-03 - Cine

    This just saved me a lot of time; thanks!

  5. 2011-06-06 - Justin

    This handy code does have an issue.

    If you’re embedding video in the excerpted area, code within or can get truncated with mixed results. Working on a solution that respects these code blocks.

  6. 2011-01-30 - Nicholas

    I think you also have to add this, no?:

    add_shortcode(‘get_the_content_with_formatting’, ‘get_the_content_with_formatting’);

  7. 2010-10-21 - Kevin

    i wondered why next gen gallery shows the tag instead of the gallery. i searched for a solution and here it is! BIG THX!!! =)

  8. 2010-09-12 - Alex H

    Thank you!!!

    I was almost there, I just needed the

    $content = str_replace(‘]]>’, ‘]]>’, $content);


    Thanks again

  9. 2010-07-22 - Galder

    That was what I was looking for. Thanksss!!!

  10. 2010-06-08 - Awats

    This just saved me 2 hours of reading through core WP files. You rule!

  11. 2010-04-29 - Edgar

    perfect solution, thanks.

  12. 2010-03-04 - Tim Bednar

    Thanks, very helpful, saved me a lot of research and work.


  13. 2010-02-04 - Hikari

    Do you know why the_content() replaces ]]> by ]]>?

    I’m searching for the reason and Google linked your post. This str_replace() is breaking my JavaScript :(

  14. 2009-11-15 - Dave

    Thank you sir, very useful

  15. 2009-06-16 - Shane

    This has been very useful – thank you. It does make you wonder, however, why this sort of stuff isn’t standard in wordpress. One day, maybe.

  16. 2009-05-26 - Franco

    Thank you very much :)

  17. 2009-04-17 - pxldot

    Oh, yeahhh… works great, thxs!

  18. 2009-03-22 - Zoinks! Graphics

    Thanks, Jenst, this is fantastic. And it works on older versions of WP too!

    Michael, regarding your question about “Is there a way to get just a snippet of the get_the_content part? Like say the first 30 characters?”… I strip the tags off a call of the content — in this case using get_the_content_with_formatting — then do an explode using spaces, then pull it back together with the first “X” words of the resultant array. Hope that helps!

  19. 2009-03-03 - Oscar

    Works like a charm ;) Thanks

  20. 2009-02-03 - Chris

    You are a star!

    Thanks solved my problem!



  21. 2009-01-21 - Lewis Litanzios

    My hero!


  22. 2009-01-13 - alex

    wow great work!
    you just saved me a lot of work. thank you so much!

  23. 2008-12-22 - James

    Nice one! Cheers.

  24. 2008-12-21 - Tim

    Thanks! Another idea is to use the output buffering to grab the string directly from the_content, in order to be safe from future updates.

  25. 2008-12-01 - Mark

    Thanks for that. Exactly what I was looking for!

  26. 2008-11-03 - Chris

    This was tremendously helpful to me, thanks!

  27. 2008-09-24 - Michael

    Hi! Cool post!
    Is there a way to get just a snippet of the get_the_content part? Like say the first 30 characters?
    Unfortunately the_content_rss doesn’t work on my version, and I’ve resorted to using get_the_content.

    Thanks for any advice.

  1. Why ob_start() is a great function | Dawn at Dusk
    Geek, Sometime Ninja – Wordpress: Formatting get_the_content() return data
    The Gearwheel » Abbreviations automatisch hinzufügen
    Jobbar med Heakrogens webb | - Webbdesign m.m.
    Insert Google Adsense Code Anywhere In Your Wordpress Article - FlexiJourney Blog
    - FlexiJourney Blog