The Excerpt Reloaded: Another Reason Why Tags Aren’t Getting Closed

June 5th, 2007

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.

26 Comments

  1. Miriamon 06 Jun 2007 at 12:10 am

    You’re a genius! I activated the latest version of the plugin on my site, and now my archives look normal! Thanks so much.

  2. […] 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 […]

  3. […] 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… […]

  4. Miriamon 06 Jun 2007 at 2:03 am

    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

  5. davidon 20 Jun 2007 at 5:30 am

    great mod of the plugin. thanks!

  6. […] Anyway, here’s the original WP plugin: The Excerpt Reloaded And here’s the updated version: The Excerpt Reloaded 2 […]

  7. Cezary Kobyli?skion 12 Jul 2007 at 9:36 am

    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?

  8. Nyssaon 28 Jul 2007 at 8:05 am

    Thanks for the fix on this plugin. Now my code validates and there aren’t any display issues. 🙂

  9. […] 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 […]

  10. Klauson 01 Feb 2008 at 5:59 am

    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

  11. Davidon 24 Apr 2008 at 6:54 pm

    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

  12. Hendrawanon 12 May 2008 at 5:53 am

    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.

  13. bobon 21 Oct 2008 at 3:12 pm

    much appriciated fix – thank you

  14. Joshon 16 Nov 2008 at 9:44 pm

    Thanks for the fix Rob. Appreciated.

    I noticed the plugin doesn’t actually link to more tag in the past (i.e. the user clicks to read more but they get taken to the top of the post instead of the anchor tag). I noticed this is determined by the last part of the code:

    if ($use_more_link && $more_link_text) {
    if($force_more) {
    $output .= ' ID) . '#more-' . $post->ID .'" title="' . $more_link_title . '">' . $more_link_text . '' . "\n";
    } else {
    $output .= ' ID) . '" title="' . $more_link_title . '">' . $more_link_text . '' . "\n";
    }
    }

    It seems the $use_more_link && $more_link_text condition is never filled because it always reverts to the else argument. I have set a more tag in the post and define the more link text with the the_excerpt_reloaded argument in the template. What could be the reason for this Rob?

  15. ~Jerry | Free Classifiedson 18 Nov 2008 at 8:56 am

    Good one.. 🙂 Could you advice how to automatically remove (strip) all html tags from excerpt using your plugin. Any real help appreciated. Cheers

  16. Robon 21 Nov 2008 at 2:24 pm

    To strip all tags, just provide an empty string for the “allow” parameter as “”.

  17. Joshon 22 Nov 2008 at 3:30 am

    Rob, can you provide a solution for my question above?

  18. Robon 22 Nov 2008 at 8:27 am

    Josh: Your question is more detailed and involves a little investigation. When I have some time, I can look into it. If you leave me another comment and fill in your email address (it won’t be posted online, only I will see it), I can email you when I have the solution. It may be a while.

  19. Joshon 24 Nov 2008 at 2:35 am

    Thanks! 😀

  20. lutfion 30 Apr 2009 at 7:54 am

    Hello rob, great plugin.
    I get one question, How to display the images only on the excerpt?
    Thank you.

  21. Amieon 05 Nov 2009 at 3:24 pm

    I updated my excerpt reloaded file with your new file….in an attempt to correct the problem of all formatting tags being stripped from the home page excerpts. I have tried every fix I can find and I still have no formatting on the home page excerpts. If you have a moment, could you please check and see if you might know what the problem is?

    Thank You

  22. Tariqon 11 Nov 2009 at 10:31 am

    Hi there,

    thanks for the updates, if you take a look at my news index page im having a problem where the Read More… is apearing on the next line, how can i get that text to appear after the last line. For example

    Text here (Read More…)

    thanks

  23. nichiveon 04 Feb 2010 at 8:24 am

    I’m facing a difficulties here, the excerpt shows the WP’s caption tag for image as such:

    [caption id="attachment_311" align="alignleft" width="258" caption="Some Image"]

    How can I solve so that this kind of tag, doesn’t come along with the excerpt?

  24. NLP Trainingon 10 Jun 2010 at 6:41 pm

    I don’t have deep knowledge with the use of these tags. Glad you’ve discussed everything in detailed here. This can really help me for now.

  25. […] customize the excerpt length.Enter The Excerpt Reloaded. The plugin was 5 years old, so I found an updated version with bugfixes that was only 3 years old. I quickly wrote this code for my theme’s index.php […]

  26. […] mod to the Excerpt Reloaded Plugin Deena Levenstein | June 6, 2007 | 1 Comment A new mod to the Excerpt Reloaded Plugin means that now you can make sure that your excerpts are properly formatted with links, bullets, […]

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.