<?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; the_content()</title>
	<atom:link href="http://rider.sofarider.com/tag/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>
	</channel>
</rss>
