How to fix get_the_content() stripped tags?

This is the problem I came across recently and I am still not sure whether this is a kind of intentional bug or else because it just looks like so. OK, what exactly happens when you use WordPress get_the_content() function in order to ’save’ Post or Page content into variable? You can do a little test. Somewhere in your Post details, after the_content() function try to add the following code – just to compare what’s happening on frontend:


<?php
	the_content(); // this is your standard way to output Post or Page content
	// now the test
	$var_content = get_the_content();
	echo $var_content;
?>

It looks like paragraphs and break rules (<p> and <br />) are gone. Maybe some other tags too but I have noticed those two. I suppose not so many developers use get_the_content() function, however some of my themes (this one too) needs it in order to extract images or documents out of Post/Page content and still keep text formatted. We all know that the_excerpt() function will strip down each and every html tag so there’s no sense in using it, especially not in Post/Page details template.

Solution

It’s true there’s a solution at codex.wordpress.org but for those who have hard time to find it – here it is:


<?php
	the_content(); // this is your standard way to output Post or Page content
	// now the test which works
	$var_content = get_the_content();
	$var_content = apply_filters( 'the_content', $var_content );
	$var_content = str_replace( ']]>', ']]>', $var_content );
	echo $var_content;
?>

It should be OK now. Just wanted to pay your attention to this issue since WordPress developer team didn’t fix it until 2.8.4 – currently available for download. Maybe next time.

Share with others...

deliciousdiggreddittechnoratifacebooktwittergoogleyahoowikioblinklistsimpyspurl
Comments (1)

Track comments via RSS 2.0 feed. Feel free to post the comment, or trackback from your web site.

  • Martin on Mar 11, 2010

    Greate tweak. Thanks!

Leave a Comment

Latest comment by Martin

Greate tweak. Thanks!

Search

Advertisement

GraphicRiver ThemeForest