Fixing the_excerpt

Over on Spamtacular, I’ve had some problems with are based around my somewhat heavy use of Academic Press (a plugin that I use for creating citations and the bibliography you’ll find at the bottom of many posts).

My problem is that WordPress doesn’t parse shortcodes when putting together the excerpts that are displayed on the homepage. So, my excerpts were reading very strangely. For instance, the excerpt for the current first post on Spamtacular read:

The   announced today that they have issued their first penalty against someone for sending email in violation of Canada’s anti-spam law.

The actual post reads:

The Canadian Radio-television and Telecommunications Commission announced today that they have issued their first penalty against someone for sending email in violation of Canada’s anti-spam law.

The reason is that the CRTC was the author of a press release that I was citing using Academic Press, so it was enclosed in a shortcode. When WordPress pulled out the excerpt to use, it stripped out the shortcode and everything enclosed within it. What’s more is that the actual text used in the text reads “{author}” (which means that it pulls the information from the shortcode).

What I needed to do was actually execute the shortcode. The additional difficulty is found in my use of Zemata’s Editorial Assistant. I use that to attach pictures to posts, usually at the top of the post. The difficulty is found in that I don’t want the image captions to show up in the displayed excerpts.

The obvious answer that everyone seems to give is to add the following to functions.php:

add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode');

Unfortunately, that wouldn’t help as that would also serve to add the caption from the image at the beginning of the post. Adding that to the excerpt wouldn’t help anyone reading the front page of my blog at all.

Eventually, I found a blog post entitled “Improving WordPress' the_excerpt() template tag.” Included in that post was the following:

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[^>]*?>.*?@si', '', $text);
$text = strip_tags($text, '
$excerpt_length = 80;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_push($words, '[...]');
$text = implode(' ', $words);
return $text;

That eventually turned into what I needed:

function custom_excerpt($text = '') {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
// $text = strip_shortcodes( $text );
$text = preg_replace("/\[caption.*\[\/caption\]/", "", $text);
$text = do_shortcode( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters('excerpt_length', 55);
//$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); }
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'get_the_excerpt', 'custom_excerpt' );

The important bit here is the line that says:

$text = preg_replace("/\[caption.*\[\/caption\]/", "", $text);

That line takes the caption from the image and replaces it with nothing.

Even after putting the code together that should have worked, I had another problem: The code wasn’t executing in my theme (SuevaFree). Eventually, on a lark, I tried putting the code into the functions.php file of a child theme.

Success! Now, my excerpts read like they should, both on the home page AND in the RSS feed.


  1. Aaron Russell (2008, November 13), Improving WordPress' the_excerpt() template tag. Retrieved March 7, 2015 from http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/.

Leave a Reply

Your email address will not be published. Required fields are marked *