This is what I added in my theme’s functions.php file to enable formatting for excerpts in WordPress (source of the tip):
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'bwp_trim_excerpt');
function bwp_trim_excerpt($text)
{
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text, '<em><strong><i><b><a><code>');
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$words = preg_split("/[nrt ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
The problem with enabling formatting in post excerpts seems to be that, if the text that is formatted by a tag (be it <b>
, <strong>
, <i>
, <em>
, or any other for that matter) is cut off by the excerpt in between, your whole page’s formatting will be overridden by that tag. Take a look at this screenshot for instance:
I believe thatâs the reason why formatting for excerpts isnât enabled by default. Is there anyway to fix this? Is something wrong with the code?
Hope I can get some help here. Thanks!
This is one of the reasons why HTML markup is removed from excerpts in the first place, to prevent such issues like this from occurring however, where there’s a will, there’s a way…
You can, through using regular expression, close the open tags applicable to the excerpt only and you might want to take a look at the following link for some ideas,
Close HTML Tags
Or alternatively you can use this plugin ready made for WordPress,
Advanced Excerpt
Or if you feel so inclined you can modify it or sample its structure and apply it to your function.
UPDATE
I decided to run a test, however please note that I’ve used a different function that I often use when creating excerpts with customizable lengths on the fly;
Put this into your functions.php file,
followed by,
Then in your theme you would do the following,
Where 55 = the length in words that you want your excerpt to be.
If you want to make use of the ACTUAL excerpt box within the post edit screen then you can also add this snippet to your functions file,
And its usage would be,
However if using the ACTUAL excerpt box in the post edit screen you would have to manually write your
<strong>,<em>,<i>,<a>,etc..
tags of course! Unless you modify the default TinyMCE for the excerpt box.So there you have it, you are covered in both instances, either…
1) grabbing an excerpt from the_content()
2) grabbing an excerpt from the_excerpt()
NOTE There might be a more efficient way to go about this by writing the functionality of the Close HTML Tags example by Milan in-case you wish to investigate that further.
It’s as simple as adding this function to your theme’s functions.php file. The code has been commented clearly, so it’s self-explanatory:
EDIT: And make sure, HTML minification is turned off in W3 Total Cache (if you are using it).
[Source Article]