Perfect Excerpt in the WordPress Way

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’t have one, sadly.

There is only an the_excerpt tag for use within The Loop. It’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 = 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);
}

See the bad code? I don’t know who wrote this, but the line “$output = $post->post_excerpt;” should be placed after the if block. OK, this is out of topic :) .

Generally the function retrieves the explicit post excerpt and apply filters “get_the_excerpt” and “the_excerpt”. What is the explicit excerpt? When you’re writing or editing a post, its input is right below your editing area. I never wrote one, though. Most bloggers don’t write excerpts, I believe. So what happens when we apply filter “get_the_excerpt” to an empty string?

Let’s have a look at the code in wp-includes/default-filters.php:

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');

By default, the string is processed only by the function wp_trim_excerpt (defined in wp-includes/formatting.php) before applying “the_excerpt” filters:

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(']]>', ']]>', $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;
}

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 “clean” text.

So I wrote this helper function for use in the plugin:

function tlnpp_excerpt($text, $excerpt_length = 55) {
	$text = str_replace(']]>', ']]>', $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);
}

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’s, for example). It truncates text by words not characters, and the result is safe.

For more information, have a look at the code of the plugin. It’s very simple. Any suggestions?

Tags:

One Response to “Perfect Excerpt in the WordPress Way”

  1. Kevin Says:

    nice except!but how to adapt to the chinese character,it didn’t work for chinese characters.

Leave a Reply

Please copy the string BrjaGi to the field below:


× 2 = twelve