<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SofaRider &#187; get_the_content()</title>
	<atom:link href="http://rider.sofarider.com/tag/get_the_content/feed/" rel="self" type="application/rss+xml" />
	<link>http://rider.sofarider.com</link>
	<description>WordPress Themes, Widgets, Development &#124; WEB &#38; Graphic Design</description>
	<lastBuildDate>Wed, 14 Oct 2009 08:40:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to trim the_content()</title>
		<link>http://rider.sofarider.com/blog/wordpress-tips/how-to-trim-the_content/</link>
		<comments>http://rider.sofarider.com/blog/wordpress-tips/how-to-trim-the_content/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 09:12:21 +0000</pubDate>
		<dc:creator>Feeleep</dc:creator>
				<category><![CDATA[Wordpress Tips]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[get_the_content()]]></category>
		<category><![CDATA[the_content()]]></category>
		<category><![CDATA[trim HTML]]></category>

		<guid isPermaLink="false">http://rider.sofarider.com/?p=307</guid>
		<description><![CDATA[My latest work was a customization of Rooster theme for one of my clients who didn&#8217;t like the idea of using &#60;!&#8211;more&#8211;&#62; separator. He wanted to have a split content at certain number of words and the idea of using the_excerpt() didn&#8217;t work hence the fact that this function strips out all the HTML code. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rider.sofarider.com/wp-content/uploads/2009/09/hoof_trim-200x200.jpg" alt="Trim content" title="Trim content" width="200" height="200" class="alignnone size-thumbnail wp-image-308" />My latest work was a customization of <a href="http://rider.sofarider.com/work/wordpress-themes/rooster-power-blog-wordpress-theme/">Rooster theme</a> for one of my clients who didn&#8217;t like the idea of using &lt;!&#8211;more&#8211;&gt; separator. He wanted to have a split content at certain number of words and the idea of using <strong>the_excerpt()</strong> didn&#8217;t work hence the fact that this function strips out all the HTML code. So I was faced with the problem and major issue was how to trim HTML formatted text?<span id="more-307"></span> Why? Because HTML formatted text may contain tags which MUST BE CLOSED and in the same time HTML tags are counted as characters!<br />
As you guess, the task may look like this: </p>
<ul>
<li>Temporary remove all eventual HTML tags</li>
<li>Trim stripped content at desired words length</li>
<li>Get all HTML tags back on their initial positions</li>
<li>Watch for unclosed tags </li>
</ul>
<p>OK, I guess the task is quite clear. Now we gonna create a function, paste it at the bottom of &#8216;functions.php&#8217; so it can be (re)used whenever is needed. I didn&#8217;t want to bother with making widget or plugin coz it takes more time and does the same job. Maybe one day I will. Here is our function:</p>
<pre>
<code>
&lt;?php
function trim_the_content( $the_contents, $read_more_tag = ' READ MORE...', $perma_link_to = '', $all_words = 45 ) {
	// make the list of allowed tags
	$allowed_tags = array( 'a', 'abbr', 'b', 'blockquote', 'b', 'cite', 'code', 'div', 'em', 'fon', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'label', 'i', 'p', 'pre', 'span', 'strong', 'title', 'ul', 'ol', 'li', 'object', 'embed' );
	if( $the_contents != '' ) {
		// process allowed tags
		$allowed_tags = '&lt;' . implode( '&gt;&lt;', $allowed_tags ) . '&gt;';
		$the_contents = str_replace( ']]&gt;', ']]&amp;gt;', $the_contents );
		$the_contents = strip_tags( $the_contents, $allowed_tags );
		// exclude HTML from counting words
		if( $all_words &gt; count( preg_split( '/[\s]+/', strip_tags( $the_contents ), -1 ) ) ) return $the_contents;
		// count all
		$all_chunks = preg_split( '/([\s]+)/', $the_contents, -1, PREG_SPLIT_DELIM_CAPTURE );
		$the_contents = '';
		$count_words = 0;
		$enclosed_by_tag = false;
		foreach( $all_chunks as $chunk ) {
			// is tag opened?
			if( 0 &lt; preg_match( '/&lt;[^&gt;]*$/s', $chunk ) ) $enclosed_by_tag = true;
			elseif( 0 &lt; preg_match( '/&gt;[^&lt;]*$/s', $chunk ) ) $enclosed_by_tag = false;
			// get entire word
			if( !$enclosed_by_tag &amp;&amp; '' != trim( $chunk ) &amp;&amp; substr( $chunk, -1, 1 ) != '&gt;' ) $count_words ++;
			$the_contents .= $chunk;
			if( $count_words &gt;= $all_words &amp;&amp; !$enclosed_by_tag ) break;
		}
                // note the class named 'more-link'. style it on your own
		$the_contents = $the_contents . '&lt;a class=&quot;more-link&quot; href=&quot;' . $perma_link_to . '&quot;&gt;' . $read_more_tag . '&lt;/a&gt;';
		// native WordPress check for unclosed tags
		$the_contents = force_balance_tags( $the_contents );
	}
	return $the_contents;
}
?&gt;
</code>
</pre>
<h2>How to use this code with your own theme templates?</h2>
<p>That&#8217;s easy, the only thing you need to do is to call our function (pasted in functions.php, remember?) and pass required arguments or parameters. For example&#8230;instead of using either <strong>the_excerpt()</strong> or <strong>the_content()</strong> WP&#8217;s functions you would write something like this:</p>
<pre>
<code>
&lt;?php
	// find out your post permalink
	$perma_link = get_permalink( $post-&gt;ID );
	// save original post content to variable
	$content = get_the_content();
	// prevent tags strip | it's a bug in WordPress!
	$content = apply_filters( 'the_content', $content );
	$content = str_replace( ']]&gt;', ']]&amp;gt;', $content );
	// throw out trimmed: content to process, read more tag, post permalink, words length
	echo trim_the_content( $content, 'READ MORE...', $perma_link, 45 );
?&gt;
</code>
</pre>
<p>Hopefully you find this function useful so share it with others PLEASE! Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://rider.sofarider.com/blog/wordpress-tips/how-to-trim-the_content/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How to fix get_the_content() stripped tags?</title>
		<link>http://rider.sofarider.com/blog/wordpress-tips/how-to-fix-get_the_content-stripped-tags/</link>
		<comments>http://rider.sofarider.com/blog/wordpress-tips/how-to-fix-get_the_content-stripped-tags/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 06:49:04 +0000</pubDate>
		<dc:creator>Feeleep</dc:creator>
				<category><![CDATA[Wordpress Tips]]></category>
		<category><![CDATA[get_the_content()]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[missing tags]]></category>
		<category><![CDATA[stripped tags]]></category>

		<guid isPermaLink="false">http://rider.sofarider.com/?p=293</guid>
		<description><![CDATA[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 &#8217;save&#8217; Post or Page content into variable? You can do a little [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rider.sofarider.com/wp-content/uploads/2009/09/1201373442_anus-200x200.jpg" alt="1201373442_anus" title="1201373442_anus" width="200" height="200" class="alignnone size-thumbnail wp-image-294" />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 <strong>get_the_content()</strong> function in order to &#8217;save&#8217; Post or Page content into variable? You can do a little test. Somewhere in your Post details, after <strong>the_content()</strong> function try to add the following code &#8211; just to compare what&#8217;s happening on frontend:</p>
<pre>
<code>
&lt;?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;
?&gt;
</code>
</pre>
<p>It looks like paragraphs and break rules (&lt;p&gt; and &lt;br /&gt;) are gone. Maybe some other tags too but I have noticed those two. I suppose not so many developers use <strong>get_the_content()</strong> 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 <strong>the_excerpt()</strong> function will strip down each and every html tag so there&#8217;s no sense in using it, especially not in Post/Page details template.</p>
<h2>Solution</h2>
<p>It&#8217;s true there&#8217;s a solution at codex.wordpress.org but for those who have hard time to find it &#8211; here it is:</p>
<pre>
<code>
&lt;?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( ']]&gt;', ']]&gt;', $var_content );
	echo $var_content;
?&gt;
</code>
</pre>
<p>It should be OK now. Just wanted to pay your attention to this issue since WordPress developer team didn&#8217;t fix it until 2.8.4 &#8211; currently available for download. Maybe next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://rider.sofarider.com/blog/wordpress-tips/how-to-fix-get_the_content-stripped-tags/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
