Improving WordPress' the_excerpt() template tag
Are you having trouble with your excerpts? Is WordPress misbehaving and making your theme look downright shoddy when all you want to do is use it’s native excerpt function? You’re not alone so read on.
Wordpress’ the_excerpt()
template tag is used in most themes for browsing the archives and categories of a blog. Rather than displaying the full content of the post, the excerpt displays a short snippet of the content. Unless you manually enter in an excerpt when writing each post, WordPress grabs the first 55 words of the post and uses that as the excerpt.
So far so good, but there are problems with the way WordPress does this. These include:
- Word count - 55 words is a good number, but what if you want more or less?
- Formatting - WordPress strips out all HTML tags. This gets rid of images and links, but can also get rid of paragraph formatting, making the entire excerpt one long paragraph without any line breaks.
- JavaScript - Unfortunately JavaScript isn't stripped out, which can result in some plugins' messy script appearing in your excepts. Not only does this look rubbish, it can be a vulnerability too.
There are ways to fix these problems, and I will show you how. Better yet, these changes can be done completely within your theme without having to hack away at the WordPress core files.
Finding the excerpt function
The first thing we need to do is find the relevant excerpt function that is not doing its job very well. Actually, you don’t need to find it because I’ve done it for you, but if you insist you can find the code below deep within wp-includes/formatting.php
.
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;
}
Remember, we are not altering the core files here, so we need to copy this code and paste it into our theme files. Open up your functions.php
theme file and paste the code in.
This function is called wp_trim_excerpt()
but it is important that we call it something unique. I’m going to rename it improved_trim_excerpt()
but you can call it what you want. Edit line one accordingly:
function improved_trim_excerpt($text) {
Increase the word length of the excerpt
This is simple. Find line eight and change the value to whatever you want - lets say 80.
$excerpt_length = 80;
Include HTML tags
If you have problems with your formatting you may need to include the P
tag. You may want to include links or even images. Find line seven and replace it with this:
$text = strip_tags($text, '<p>');
You can simply list as many tags as you need to immediately following the P
tag.
Remove unwanted JavaScript code
To remove unwanted script code you need to add another line. In-between lines five and six add the following:
$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
OK, that’s sorted out all our problems. Just to clarify your new and improved function should look like this:
function improved_trim_excerpt($text) {
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
$text = strip_tags($text, '<p>');
$excerpt_length = 80;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
}
return $text;
}
The final thing we need to do is tell WordPress to use our new and improved function rather than its built in function. Fortunately this is a piece of cake and involves entering two more lines at the end of our functions.php
theme file.
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');
That’s everything complete. You’re now set for improved excerpts.
This article was originally published on miLienzo.com on 2 September 2007.