Mickey Chandler

My occasional barrel of digital ink.

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 “[acp author=”Aaron Russell” title=”Improving WordPress’ the_excerpt() template tag” id=”Russell_01″ url=”http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/” media=”blog” year=”2008″ month=”November” day=”13″ year_access=”2015″ month_access=”March” day_access=”7″]{title}[/acp].” 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_pop($words);
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.

About the Author

Mickey Chandler Consultant & Attorney

Mickey Chandler is a Consultant & Attorney with over 28 years of experience in Email Deliverability & Privacy Law. He has a strong background in email authentication infrastructure (SPF, DKIM, DMARC), ISP and mailbox provider relations, anti-spam policy and compliance, CAN-SPAM and state anti-spam law gained through overseeing the Abuse & Compliance team at Salesforce Marketing Cloud, originating the ISP relations role at Informz (now part of Higher Logic), and working in the fight against spam since 1997. He holds a B.A. in Government, a B.S. in Computer Information Systems, and a J.D. from the University of Houston Law Center. He is a certified CIPP/US professional and a certified CIPM professional. He has speaker at M3AAWG, podcast guest.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Make your website live today!

GET A FULL COPY OF THIS EXACT DEMO THEME IN YOUR WORDPRESS WITHIN MINUTES.

Purchase this WordPress theme today!

Buy Food Travel Blog Theme SALE