<?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>Thin &#38; Light &#187; excerpt</title>
	<atom:link href="http://thinlight.org/tag/excerpt/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinlight.org</link>
	<description>Passion for the web</description>
	<lastBuildDate>Sun, 30 May 2010 06:04:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Perfect Excerpt in the WordPress Way</title>
		<link>http://thinlight.org/2008/01/15/perfect-excerpt-in-the-wordpress-way/</link>
		<comments>http://thinlight.org/2008/01/15/perfect-excerpt-in-the-wordpress-way/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 17:29:51 +0000</pubDate>
		<dc:creator>thinlight</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[excerpt]]></category>

		<guid isPermaLink="false">http://thinlight.org/2008/01/15/perfect-excerpt-in-the-wordpress-way/</guid>
		<description><![CDATA[When I started working on the Neighbor Post Preview plugin, I expected WordPress to have a built-in function to get an excerpt from an arbitrary post. But it doesn&#8217;t have one, sadly.
There is only an the_excerpt tag for use within The Loop. It&#8217;s defined in wp-includes/post-template.php (version 2.3.2 code):

function the_excerpt() {
	echo apply_filters('the_excerpt', get_the_excerpt());
}

function get_the_excerpt($deprecated = [...]]]></description>
			<content:encoded><![CDATA[<p>When I started working on the <a href="http://thinlight.org/2008/01/13/wordpress-neighbor-post-preview-plugin/">Neighbor Post Preview</a> plugin, I expected WordPress to have a built-in function to get an excerpt from an arbitrary post. But it doesn&#8217;t have one, sadly.</p>
<p>There is only an <a href="http://codex.wordpress.org/Template_Tags/the_excerpt">the_excerpt</a> tag for use within The Loop. It&#8217;s defined in wp-includes/post-template.php (version 2.3.2 code):</p>
<pre>
<code>function the_excerpt() {
	echo apply_filters('the_excerpt', get_the_excerpt());
}

function get_the_excerpt($deprecated = true) {
	global $id, $post;
	$output = '';
	$output = $post->post_excerpt;
	if ( !empty($post->post_password) ) { // if there's a password
		if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
			$output = __('There is no excerpt because this is a protected post.');
			return $output;
		}
	}

	return apply_filters('get_the_excerpt', $output);
}</code></pre>
<p>See the bad code? I don&#8217;t know who wrote this, but the line &#8220;$output = $post->post_excerpt;&#8221; should be placed after the if block. OK, this is out of topic <img src='http://thinlight.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<p>Generally the function retrieves the <strong>explicit</strong> post excerpt and apply filters &#8220;get_the_excerpt&#8221; and &#8220;the_excerpt&#8221;. What is the explicit excerpt? When you&#8217;re writing or editing a post, its input is right below your editing area. I never wrote one, though. Most bloggers don&#8217;t write excerpts, I believe. So what happens when we apply filter &#8220;get_the_excerpt&#8221; to an empty string?</p>
<p>Let&#8217;s have a look at the code in wp-includes/default-filters.php:</p>
<pre><code>add_filter('the_excerpt', 'wptexturize');
add_filter('the_excerpt', 'convert_smilies');
add_filter('the_excerpt', 'convert_chars');
add_filter('the_excerpt', 'wpautop');
add_filter('get_the_excerpt', 'wp_trim_excerpt');</code></pre>
<p>By default, the string is processed only by the function <em>wp_trim_excerpt</em> (defined in wp-includes/formatting.php) before applying &#8220;the_excerpt&#8221; filters:</p>
<pre><code>function wp_trim_excerpt($text) { // Fakes an excerpt if needed
	global $post;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]&gt;', ']]&gt;', $text);
		$text = strip_tags($text);
		$excerpt_length = 55;
		$words = explode(' ', $text, $excerpt_length + 1);
		if (count($words) > $excerpt_length) {
			array_pop($words);
			array_push($words, '[...]');
			$text = implode(' ', $words);
		}
	}
	return $text;
}</code></pre>
<p>If the string is empty, the excerpt is faked using the content of the global $post. After that, the excerpt is filtered by some functions to make some &#8220;clean&#8221; text.</p>
<p>So I wrote this helper function for use in the <a href="http://thinlight.org/projects/wordpress-neighbor-post-preview/">plugin</a>:</p>
<pre><code>function tlnpp_excerpt($text, $excerpt_length = 55) {
	$text = str_replace(']]&gt;', ']]&gt;', $text);
	$text = strip_tags($text);
	$words = explode(' ', $text, $excerpt_length + 1);
	if (count($words) > $excerpt_length) {
		array_pop($words);
		array_push($words, '[...]');
		$text = implode(' ', $words);
	}

	return apply_filters('the_excerpt', $text);
}</code></pre>
<p>This function truncate an arbitrary piece of text to 55 words by default, and process the result using the default WordPress filter functions (converts smiley&#8217;s, for example). It truncates text by words not characters, and the result is safe.</p>
<p>For more information, have a look at the code of the plugin. It&#8217;s very simple. Any suggestions?</p>
]]></content:encoded>
			<wfw:commentRss>http://thinlight.org/2008/01/15/perfect-excerpt-in-the-wordpress-way/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
