<?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; trim html</title>
	<atom:link href="http://rider.sofarider.com/tag/trim-html/feed/" rel="self" type="application/rss+xml" />
	<link>http://rider.sofarider.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Tue, 26 Oct 2010 08:40:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to auto-trim the_content()?</title>
		<link>http://rider.sofarider.com/wordpress-tips-and-tricks/how-to-trim-the_content-automatically/</link>
		<comments>http://rider.sofarider.com/wordpress-tips-and-tricks/how-to-trim-the_content-automatically/#comments</comments>
		<pubDate>Sun, 19 Sep 2010 07:13:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[WordPress Tips and Tricks]]></category>
		<category><![CDATA[automatic read more link]]></category>
		<category><![CDATA[trim html]]></category>
		<category><![CDATA[trim the_content]]></category>

		<guid isPermaLink="false">http://rider.sofarider.com/?p=91</guid>
		<description><![CDATA[There are two &#8220;official&#8221; methods of how to display Post excerpt: by using built-in text editor &#8220;Insert More tag&#8221; (Ctrl+Shift+T) or WordPress function named &#8220;the_excerpt()&#8221;. By the way, Post excerpt is actually a chunk of text containing that &#8220;MORE&#8221; link at the end. None of these two offer an elegant solution &#8211; that&#8217;s my personal <a href="http://rider.sofarider.com/wordpress-tips-and-tricks/how-to-trim-the_content-automatically/" class="more-link">READ MORE</a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://rider.sofarider.com/wp-content/uploads/2010/09/trim-274x150.jpg" alt="trim post content" title="trim post content" width="274" height="150" class="alignnone size-thumbnail wp-image-95" />There are two &#8220;official&#8221; methods of how to display Post excerpt: by using built-in text editor &#8220;Insert More tag&#8221; (Ctrl+Shift+T) or WordPress function named &#8220;the_excerpt()&#8221;. By the way, Post excerpt is actually a chunk of text containing that &#8220;MORE&#8221; link at the end. None of these two offer an elegant solution &#8211; that&#8217;s my personal opinion.<br />
<strong>The first one</strong> is partially OK because it allows users to insert &#8220;MORE&#8221; link at desired position in text and HTML doesn&#8217;t get stripped on front-end. However, people often forget to insert more tag or hate counting words in order to keep Post excerpt consistent regarding the amount of text.<br />
<strong>The second method</strong> is usually built into certain template file (the_excerpt() function) and is supposed to automatically trim Post content to 55 words. It sounds fine but you gotta know that this function strips down all of HTML tags including images.</p>
<blockquote><p>So how about to simply trim the_content() to desired number of words, keep all of HTML tags and no need for More tag insertion at all?</p></blockquote>
<p>Here&#8217;s the function I use for all of my themes and it works perfectly&#8230;</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', 'param' );
	if( $the_contents != '' &amp;&amp; $all_words &gt; 0 ) {
		// process allowed tags
		$allowed_tags = '&lt;' . implode( '&gt;&lt;', $allowed_tags ) . '&gt;';
		$the_contents = str_replace( ' ]]&gt;', ' ]]&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 . '<a class="more-link" rel="nofollow" href="' . $perma_link_to . '">' . $read_more_tag . '</a>';
		// native WordPress check for unclosed tags
		$the_contents = force_balance_tags( $the_contents );
	}
	return $the_contents;
}
?&gt;
</code>
</pre>
<h2>Where to paste the above code?</h2>
<p>If your WP theme doesn&#8217;t contain &#8220;functions.php&#8221; file &#8211; create one and simply paste. Otherwise use existing &#8220;functions.php&#8221;.</p>
<h2>How to use the above function?</h2>
<p>Many of you will not need an additional explanation but here it is just for case&#8230;.<br />
Which template file uses More tag? As far as I know, the list includes &#8220;category.php&#8221;, &#8220;index.php&#8221;, &#8220;search.php&#8221;, &#8220;archive.php&#8221;, &#8220;tag.php&#8221; or any other custom made template file. It&#8217;s not a must &#8211; of course, but rather your choice. So if you decide to apply automatic content trim, standard call to a WP function the_content() will not do the job. You&#8217;ll have to add several extra lines of code instead:</p>
<pre><code>
&lt;?php
// here's the point where we are about to display Post excerpt
$perma_link = get_permalink( $post-&gt;ID ); // current Post permalink
$content = get_the_content(); // save entire content in variable
$content = apply_filters( 'the_content', $content ); // WP bug fix
$content = str_replace( ']]&gt;', ']]&gt;', $content ); // ...as well
echo trim_the_content( $content, __( "READ MORE", "sofa_memento" ), $perma_link, 55 ); // trim to 55 words
?&gt;
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rider.sofarider.com/wordpress-tips-and-tricks/how-to-trim-the_content-automatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

