The Excerpt Reloaded: Another Reason Why Tags Aren’t Getting Closed
Download:
the-excerpt-reloaded.zip
Update 7/17/2007: New version R1.4: Fixed the plugin URI and author URI so that the links on the plugin administration page will work.
Update 7/10/2007: I incorporated a fix that Hillary Melville described in a comment (see here) into the downloadable plugin, which is now at version R1.3.
Miriam of Wordpress Garage commented that my first update to the_excerpt Reloaded did not fix an issue she was having at her site. I found the root cause of her problem and have a fix for this issue. I have updated my fix to the plugin, and you may download it from the box at the top of this article (and also from the download link in my original post).
The Problem
Since the_excerpt Reloaded only takes the first N words of your post to create an excerpt, there is a chance that it may cut off your excerpt at a point where a tag within the excerpt doesn’t get closed. The input parameter, $fix_tags, to the_excerpt_reloaded() function is supposed to force the plugin to close any tags that this has happened to, but this doesn’t happen and tags remain unclosed. As a result of this your generated web page will not be valid XHTML.
Why It Happens
There are actually 2 reasons for this problem, and they are both caused by this PHP statement:
$output = $fix_tags ? $output : balanceTags($output);
The first issue is that the logic is reversed. If $fix_tags is true, we want the balanceTags() function to be called, therefore balanceTags() should appear right after the “?”, and not after the “:”.
The second issue is a result of the first statement inside the balanceTags() function, which is this:
if ( !$force && get_option('use_balanceTags') == 0 )
return $text;
What this means is that if $force is not TRUE, and the option “use_balanceTags” is not set, then the rest of the balanceTags code won’t be called, and the function will return what it was given without closing unclosed tags. Therefore, if your blog doesn’t meet these conditions, the function won’t close your unclosed tags.
The Fix
The balanceTags() function takes $force as a second parameter. If it is omitted, it defaults to false. Therefore, if we want to force the tags to be balanced, we can set it to true as the second parameter in the call to balanceTags().
We also need to reverse the logic of the original conditional statement if we want $fix_tags to operate the way we want ($fix_tags==1 means we want to fix the tags, not $fix_tags==0).
Therefore, we need to change this statement:
$output = ($fix_tags) ? $output : balanceTags($output);
to this:
$output = ($fix_tags) ? balanceTags($output,true) : $output;
This fix is included in the plugin download at the top of this article (and also in my original article).
It was another fun time to find the reason for this problem. Thank you Miriam for motivating me to solve this puzzle.

You’re a genius! I activated the latest version of the plugin on my site, and now my archives look normal! Thanks so much.
[…] June 6, 2007: After testing out Rob’s mod of the Excerpt Reloaded plugin, and not seeing much of a difference in the results on my archive pages, Rob went and fixed his […]
[…] A new mod to the Excerpt Reloaded Plugin means that now you can make sure that your excerpts are properly formatted with links, bullets, paragraphs and more. Read on… […]
Hi again Rob! I posted a complete review of your mod on my site, plus I modified my original posting about The Excerpt Reloaded to include mentions of your plugin.
See the new posting here:
Important Mod to the Excerpt Reloaded Plugin
See my modified original posting here:
The Excerpt Reloaded
great mod of the plugin. thanks!
[…] Anyway, here’s the original WP plugin: The Excerpt Reloaded And here’s the updated version: The Excerpt Reloaded 2 […]
It is some strange situation. The standard text created by text editor in WP is in p tag. When I set first param to 25 and post have less than 25 letters then plugin strip all default tags from post, including of course p. When post have more than 25 letters then post have all original tags. What is wrong?
Thanks for the fix on this plugin. Now my code validates and there aren’t any display issues.
[…] in it, for example, the entire page following that excerpt would be styled as one long list. But Rob, a blessed plugin developer, created a mod of the plugin that fixed this problem. I highly […]
Hi Rob,
fist of all thanks for the great Plugin, second sorry for the English from a German and last sorry I am a beginner with php.
I am using you program and I believe there is a small problem?
The version I am using is 1.4
The configuring is as followed:
‘,’excerpt’,TRUE,’(mehr….)’,TRUE,1,TRUE); ?>
If I am using short texts less than 80 words the more_tag will not be shown!!
This configuration is for me very often, because I prefer the using of the short text option of wordpress.
My Question is: I am doing something wrong?
Additional I would prefer to get the more_tag in the same line us the short text. There is a automatic word wrapping before ??
Examples of the Problem you will find on my URL: http://www.kk4you.de/kategorie/reise
This is a mix of long and short texts.
Is there a possibility to change this?
Thanks in advance
Klaus
Hello
1.
I am using this MOD, GREATTT ! the blank admin page and the “Wordpress requires Cookies but your browser does not support them or they are blocked” issue both are solved by this MOD. I hope others can benefit from this. Since I was wandering and the issue was of Excerpt_reloaded, other than anything.
2. I have another problem - when I type exceprt, It does not show the typed excerpt. + earlier version it showed typed excerpt but did not give me the more link.
3. So you need to provide solution for, showing the typed excerpt, with the customized more link.
PLEASEEEE
I’ve been using another plugin in to generate excerpt but strip all html tags. I want to keep it remains. I searched and found your solution. Great. Thanks.