The Excerpt Reloaded: The Root Cause and Fix For Creating Validated XHTML

June 2nd, 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 comment section below) into the downloadable plugin, which is now at version R1.3.

Update 6/6/2007: I found an additional cause of unclosed tags and found a fix. See this article for details. This second fix is incorporated in the plugin you can download from this page.

A WordPress plugin called the-excerpt-reloaded allows one to generate excerpts from the first N words of a wordpress post. The problem with this plugin is that it can create XHTML that doesn’t validate (some tags do not close). I determined the root cause of this problem and found a fix for this issue. You may download the updated plugin here.

The Problem

The invalid XHTML is caused when a <p> tag is inserted at the start of the excerpt, but there is no closing </p> tag. I found that many others were having this same issue (see comments here). The plugin has a parameter $fix_tags that is supposed to fix issues like this, but even with this parameter set to “true”, the problem still occurs.

Why It Happens

The problem is introduced when the function get_the_excerpt_reloaded() calls the apply_filters() function. apply_filters() performs several “filter” operations on the excerpt text in order to generate the final XHTML from a block of text. I believe that this is mainly for processing wordpress posts that are created with the WYSIWYG editor to create things like smilies or paragraph tags. One of the filter functions that it calls for “the_excerpt” filter is named “wpautop()”. This is the filter that adds the paragraph tags. This is where the problem is caused.

After the part of wpautop() where <p> and </p> pairs are inserted around text paragraphs, it does a few more search and replaces on the text before it returns the final output. Here are 2 of the search/replaces it performs:

  1. If any tag in a set of tags (call this set $allblocks) is directly after a <p> tag, with only whitespace character(s) (and no non-whitespace characters) between <p> and that opening tag, then the <p> is removed.
  2. If any tag in the $allblocks set of tags is directly before a </p> tag, with only whitespace character(s) (and no non-whitespace characters) between that tag and </p>, then the </p> is removed.

One of the tags in the $allblocks set is <div> (or </div>). By default, the_excerpt_reloaded puts a <div class=”more-link”> and </div> at the end of the excerpt containing the $more_link_text (the text that you designate that says something like “Continue Reading…”). Presumably, this is done so that you can define special formatting for the “more-link” class in your CSS.

The </div> will appear at the end of the excerpt. After the intermediate step where wpautop() places the </p> at the end of your post, it performs check #2, finds a </div> tag right before a </p> tag, and it removes the </p>. wpautop doesn’t remove the opening <p> because it is not directly next to another tag, and is most likely next to the first word of your post. This is why an opening <p> remains, while the </p> is removed!

The Fix

To fix this problem, we move the call to apply_filters() to be before adding the final <div></div> pair that encloses your $more_link_text.

First at the very end of the function, cut apply filters() from here:

$output = apply_filters($filter_type, $output);
return $output;

Next, paste the apply_filters() call at the end of this text:

$output = rtrim($output, “\s\n\t\r\0\x0B”);
$output = ($fix_tags) ? $output : balanceTags($output);
$output .= ($showdots && $ellipsis) ? ‘…’ : ”;
$output = apply_filters($filter_type, $output);

This fix is included in the updated the_excerpt_reloaded.php file attached to this post.

The $fix_tags bug

There was another bug that I noticed (as well as others) where the $fix_tags parameter is not passed from the_excerpt_reloaded() to get_the_excerpt_reloaded(). The fix for this is also included in the updated the_excerpt_reloaded() file attached to this post. To fix this, insert $fix_tags in this line:

echo get_the_excerpt_reloaded($excerpt_length, $allowedtags, $filter_type, $use_more_link, $more_link_text, $force_more, $fakeit, $no_more, $more_tag, $more_link_title, $showdots);

to make it look like this:

echo get_the_excerpt_reloaded($excerpt_length, $allowedtags, $filter_type, $use_more_link, $more_link_text, $force_more, $fakeit, $fix_tags, $no_more, $more_tag, $more_link_title, $showdots);

Also, change this line:

function get_the_excerpt_reloaded($excerpt_length, $allowedtags, $filter_type, $use_more_link, $more_link_text, $force_more, $fakeit, $no_more, $more_tag, $more_link_title, $showdots) {

to this:

function get_the_excerpt_reloaded($excerpt_length, $allowedtags, $filter_type, $use_more_link, $more_link_text, $force_more, $fakeit, $fix_tags, $no_more, $more_tag, $more_link_title, $showdots) {

Possible Other Issues and Other Features

Even with this fix, I can envision some other problems that the_excerpt_reloaded() may cause.

Even with $fix_tags set to true, opened tags that are closed prematurely because the excerpt is cut-off will not have all of the intended text between the opening and closing tag. This may or may not be an issue. It may be a good idea to cut off the opening tag (as well as all text after it) to make this cleaner.

Another issue is that when counting N words, the plugin will erase any kind of whitespace (including newlines) between each word and just create a “space”. I’m not sure if this is always an issue. It may cause paragraphs to get combined.

One feature that I would like to add is to make the $more_link_text appear as part of the last paragraph (at the end) instead of separated as it’s own paragraph.

When I have some time, I’ll see if I can make these improvements to the_excerpt_reloaded(). It was fun doing the detective work to determine the cause of the invalid XHTML problem. I hope that this fix is useful for you.

83 Comments

  1. willon 04 Jun 2007 at 1:03 am

    Thanks for the update!

  2. [...] June 5, 2007: Rob from Rob’s Notebook posted a comment, which you can see below, about his mod of the Excerpt Reloaded plugin. There is a problem with the original plugin where very often the closing paragraph tag </p> [...]

  3. Steveon 05 Jun 2007 at 3:28 am

    Cheers for the update Rob.

  4. [...] –more– pseudo-tag – the body of the code comes courtesy of the-excerpt-reloaded with modifications – for which, many [...]

  5. [...] the_excerpt Reloaded [...]

  6. Hillary Melvilleon 09 Jul 2007 at 11:51 pm

    Good work! I wish I had found this about 3hours earlier :)

    You missed this one:

    if(‘all’ != $allowed_tags) {
    $output = strip_tags($output, $allowedtags);
    }

    if(‘all’ != $allowedtags) {
    $output = strip_tags($output, $allowedtags);
    }

  7. Robon 10 Jul 2007 at 8:40 pm

    Good find Hillary! Thank you for pointing it out.

    I updated the downloadable plugin to have this fix. It is now at version R1.3.

  8. Alishaon 15 Jul 2007 at 5:43 pm

    Thank you so much for this fix! I spent quite a long time trying to figure out why it wasn’t XHTML valid!

  9. Plugins de Wordpress at Habitaquoon 05 Oct 2007 at 7:15 pm

    [...] el tema de los excerpt (”seguir leyendo”) automáticos, acabé cogiendo “the_except_reloaded” (ya modificado por otro) para poder cortar por caracteres y no por palabras, respetando las [...]

  10. [...] The Excerpt Reloaded This plugin does what I always wanted to be done: instead of your post being truncated without any option and shown plain (and boring) by the original “the_excerpt()” function of WordPress, this plugin – that was originally written by Kaf Oseo – gives you control over the length and the format of your excerpt. While truncating the post, this plugin tries to prevent tags from not being closed – it works in most cases. Sometimes, properly closing the tags does not work, which will break XHTML validity – e.g. if a link is created at the very position where the post is truncated. Whatever, it works in most cases, and I am sure they find a way of further improving the plugin. [...]

  11. ITExperience.neton 21 Nov 2007 at 4:58 pm

    Thank you for this interesting article! It has really helped me building my own website!

  12. [...] dies ist verstanden und der Tipp das Plugin the excerpt Reloaded zu verwenden für mich zu der Downloadseite mit den notwendigen Hinweisen. Nun geht es ans [...]

  13. Trishaon 21 Jan 2008 at 6:20 pm

    Using this plugin (your version) with WP 2.3.2 – is there a way to make it take the excerpt from the most recent Post, but not Pages? I use this in the sidebar and it works fine on my homepage (showing excerpt from most recent post), but when I go to a Page, this section of the sidebar now shows an excerpt from that particular Page, which is not what I want. Is there a modification I can make to stop it from including Pages? Thanks for any advice you can offer….

  14. Crystalon 23 Feb 2008 at 6:31 pm

    I using the last plugin – Thank you. I still get a 404 error when I click continue to view the entire post. Please, direct me to right place to get an answer. I’ve been working for hours on this :[

  15. Gilberton 05 Mar 2008 at 5:05 pm

    I wanted to ask if that fix goes into the plugin itself or the where the file is applied into the loop?

  16. Gilberton 05 Mar 2008 at 5:08 pm

    I downloaded the fixed plugin and uploaded it to my blog, and still the paragraphs in my main page ignore the . what can still be wrong in my version? Could someone help me?

  17. Gilberton 05 Mar 2008 at 5:16 pm

    This is what I currently have in my code:


    ‘, ‘none’, true, ‘Keep Reading…’, false, 1,0,”,”,1); ?>

    It still doesnt add the paragraph breaks.

  18. jsherkon 29 Mar 2008 at 1:43 pm

    I upgraded my sandbox to WordPress 2.5.rc2 (release candidate 2) and the_excerpt_reloaded R1.4 appears to be function fine without any glitches.

    Just wanted to let you know!!

  19. [...] little plugin for WordPress that allows me to format my excerpts all I like. It’s called The Excerpt Reloaded, and it let’s you set a ton of options like excerpt length, the name of your “read [...]

  20. Meerblickzimmeron 10 Apr 2008 at 3:18 am

    Hei!

    Thanks for your work, but it doesn´t work for me. I use your fixed Plugin and this code:

    the_excerpt_reloaded(150, ‘

    ‘, TRUE, ‘les mer »’, FALSE, 2);

    Whats wrong? Can you help me?
    Thanks a lot!
    Greetings.
    M

  21. Meerblickzimmeron 10 Apr 2008 at 3:22 am

    I´m sorry, but the my code i not right. The right code is her: http://www.mein.meerblickzimmer.de/theexcerpt.txt

    Maybe can you help me.
    Thanks a lot! M

  22. Davidon 24 Apr 2008 at 6:57 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

  23. NSpeakson 27 Apr 2008 at 1:51 am

    Does this works with WordPress 2.5?

  24. More notes on v6 - blog - coda.cozaon 05 May 2008 at 8:25 pm

    [...] the_excerpt Reloaded (more customisable than WP’s default excerpt function, it allows you to exclude elements such as images, links, etc.) [...]

  25. Dave Bergschneideron 19 May 2008 at 8:44 am

    Nice plugin! I have been using a slightly older version for a while and have finally just updated to the current version. I would seriously make a donation for the $more_link_text appear as part of the last paragraph feature. That has been my one peeve since I installed it in early 2007.

  26. Dave Bergschneideron 19 May 2008 at 8:46 am

    To answer NSpeaks question. Yes it works in WP 2.5 as I have recently upgraded.

  27. Dave Bergschneideron 19 May 2008 at 8:50 am

    I noticed several people having issues recently getting it to work correctly so here after uploading the excerpt reloaded to my plugin directory, I edited my index.php for my theme to have the following code that works for me:

    ‘, ‘content’, TRUE, ‘Read more »’, FALSE, 2); ?>

  28. Dave Bergschneideron 19 May 2008 at 8:51 am

    Forgot to escape it:

    <div class="entry">
    <?php if (is_search()) { ?>
    <?php the_excerpt() ?>
    <?php } else { ?>
    <?php the_excerpt_reloaded(100, ‘<img><p><strong>’, ‘content’, TRUE, ‘Read more &raquo;’, FALSE, 2); ?>
    <?php } ?>
    </div>

  29. Robon 21 May 2008 at 4:32 pm

    Dave or anyone else: I’d be glad to take a donation (for the right amount) to add features.

  30. [...] someone – who wrote that WordPress’s core developers are still in the world of 2003 or so. ] http://robsnotebook.com/the-excerpt-reloaded [...]

  31. [...] somewhere along the way, so the widget requires the Page Excerpt plugin to handle those. If The Excerpt Reloaded is installed, the widget will use that function; otherwise it’ll do a normal [...]

  32. Webagenturon 06 Aug 2008 at 10:46 am

    Thank you … this has me very helped.

  33. [...] empfiehlt sich auf jeden Fall, das Plugin The Excerpt Reloaded zu installieren. Hierdurch eröffnen sich diverse Möglichkeiten, den Excerpt durch [...]

  34. [...] The excerpt Reloaded [...]

  35. [...] post, that is unacceptable.After much searching, I’ve found a better way: a plugin called The Excerpt Reloaded. It lets you customize the excerpts by using a new function, the_excerpt_reloaded, instead of [...]

  36. Motherless | Mr. Nordstromon 02 Sep 2008 at 12:34 pm

    [...] here on WordPress 2.6 with latest versions of plugins FlickRSS, Twitter Tools, Time Since and The Excerpt Reloaded – phew, hope I didn’t forget anything. These plugins are required for the theme to work out [...]

  37. Tinhon 17 Sep 2008 at 10:21 pm

    It does not work for WP 2.6.2 either, how to fix it please. Thanks

  38. Stephanie Learyon 26 Sep 2008 at 9:09 am

    Having major problems in 2.6.2. I thought it might be a problem with revisions, so I turned them off using Revision Control, but that didn’t help. I can get the excerpts to show up by inserting a tag if there isn’t one, and by taking it out if there is. Bizarre.

  39. [...] a great little plugin for WordPress that allows me to format my excerpts all I like. It’s called The Excerpt Reloaded, and it let’s you set a ton of options like excerpt length, the name of your “read more” [...]

  40. ashishon 11 Dec 2008 at 12:33 pm

    Hi
    I get the following error when I try to activate the plugin: Cannot redeclare wp_the_excerpt_reloaded() in ..wp-content/plugins/the-excerpt-reloaded_1/the-excerpt-reloaded.php on line 87.

    I hv followed the fix you mentioned (apply_filters one)

  41. mammetancaloon 31 Dec 2008 at 11:52 am

    I have one problem, no matter what settings I use I always get extra … at the end of the excerpt, if I try something like Read more… it would output like … Read more…
    Any idea how to solve it, I’m using WP 2.7 and no other plugins, thank you.

  42. sdgon 31 Dec 2008 at 9:10 pm

    i installed the plugin in 2.7 but there are no options under Dashboard to set any preferences– short of editing the code — and since it isn’t doing anything at the default installation, not sure what’s going on… is it not updated for 2.7? Thanks… and happy new year

  43. [...] the_excerpt Reloaded [...]

  44. Internetagenturon 14 Feb 2009 at 4:19 am

    Thank you … that has me helped. This tutorial is very well done.

  45. [...] the plugin at http://robsnotebook.com/the-excerpt-reloaded/ [...]

  46. [...] gives you more control over the content than the WP default. More info on The Excerpt Reloaded here – make sure you use v1.4 to get validation [...]

  47. nahilion 06 Apr 2009 at 8:13 am

    For compliance with Language Switcher Plugin, from Poplarware, please add $text = apply_filters( ‘the_content’, $text ); before $text = explode(‘ ‘, $text); ,which is in the ‘else’ loop. Thanks to archas who commented here: http://robsnotebook.com/the-excerpt-reloaded2

  48. elotseon 04 Jun 2009 at 6:22 am

    Thanks first for the great work you have done.
    As I analyses the function, two problems are coming up:
    The function –force_more_link– does not work!
    I wont to use in general the more link after the text (excerpt)
    I use the following configuration together with WP 2.7:

    the_excerpt_reloaded (50,’ ‘,’excerpt’,TRUE,’[mehr...]‘,TRUE,1,TRUE);

    The second problem I wont to solve is:
    The excerpt should be displayed in one line together with the more link. Is there a solution to fix this problem.

  49. tagechoon 01 Jul 2009 at 11:28 am

    I’ve try to activate but error.
    Fatal error: Cannot redeclare wp_the_excerpt_reloaded() (previously declared in htdocs/wp-content/themes/wp-max/functions.php:355) in /htdocs/wp-content/plugins/the-excerpt-reloaded/the-excerpt-reloaded.php on line 104

    what’s wrong .

  50. [...] solucionarlo tenemos un mod de the_excerpt_reloaded, (ver actualización también aquí), que sirve para cerrar todas las tags. Este es el que [...]

  51. [...] for example, you could show five posts but include the excerpt for only the most recent. Supports The Excerpt Reloaded and Advanced [...]

  52. [...] for example, you could show five posts but include the excerpt for only the most recent. Supports The Excerpt Reloaded and Advanced Excerpt for excerpts with HTML [...]

  53. [...] are plugins for this, such as The Excerpt Reloaded. I for one always try to keep plugin usage to a minimum and this can be done very easily by adding [...]

  54. [...] for example, you could show five posts but include the excerpt for only the most recent. Supports the_excerpt Reloaded and Advanced [...]

  55. [...] for example, you could show five posts but include the excerpt for only the most recent. Supports the_excerpt Reloaded and Advanced [...]

  56. New Wordpress Plugins Release 07/27/2009on 28 Jul 2009 at 12:27 am

    [...] for example, you could show five posts but include the excerpt for only the most recent. Supports the_excerpt Reloaded and Advanced [...]

  57. [...] for example, you could show five posts but include the excerpt for only the most recent. Supports the_excerpt Reloaded and Advanced [...]

  58. [...] the excerpt-reloaded – by Kaf Oseo, Rob Bresalier http://robsnotebook.com/the-excerpt-reloaded/ [...]

  59. [...] Using The Excerpt Reloaded Plugin The Excerpt Reloaded has the following parameters that can be used to fine tune excerpts, detailed parameters will [...]

  60. [...] Using The Excerpt Reloaded Plugin The Excerpt Reloaded has the following parameters that can be used to fine tune excerpts, detailed parameters will [...]

  61. [...] Anda bisa menampilkan lima posting tetapi hanya posting terbaru saja yang diberi kutipan. Mendukung the_excerpt Reloaded dan Advanced [...]

  62. Wordpress Plugin?6+????????? | SelfCareson 16 Oct 2009 at 9:21 pm

    [...] The Excerpt Reloaded????????????XHTML??? [...]

  63. [...] little plugin for WordPress that allows me to format my excerpts all I like. It’s called The Excerpt Reloaded, and it let’s you set a ton of options like excerpt length, the name of your “read [...]

  64. [...] Posts Post-Plugin Library The_excerpt Reloaded Viva Thumb-Zoom Recent [...]

  65. Bockerlon 17 Dec 2009 at 11:02 am

    Thank you … this has me very helped.

  66. [...] from stripping the HTML formatting from your excerpts. There are two plugins you can use, however: the_excerpt Reloaded and Advanced [...]

  67. [...] from stripping the HTML formatting from your excerpts. There are two plugins you can use, however: the_excerpt Reloaded and Advanced Excerpt. Posted on February 9, 2010 at 10:14 am in Metadata, Posts and Pages, [...]

  68. Scott Carpenteron 03 Apr 2010 at 7:38 am

    Thank you! I had put in a crude fix a while back to get blockquotes to close correctly, but today discovered an anchor tag that didn’t close, and this is a much more elegant fix to both my problems. I’m glad people are still taking care of this plugin.

  69. shoaib hussainon 13 Apr 2010 at 10:15 am

    thnx a lot for such a nice plugin , keep up the good work .

  70. Wordpress Plugin?6+????????? « ???????on 25 May 2010 at 2:50 am

    [...] The Excerpt Reloaded???????????XHTML??? [...]

  71. aimon 25 May 2010 at 7:37 am

    still a good plugin, thx for sharing it!

  72. miniml press theme « siiimpleon 29 May 2010 at 5:29 pm

    [...] the excerpt-reloaded – by Kaf Oseo, Rob Bresalier http://robsnotebook.com/the-excerpt-reloaded/ [...]

  73. Ecommerce Post 1 | GB Design Studioon 25 Jul 2010 at 4:13 am

    [...] title to the post or page. Requires Page Excerpt or Excerpt Editor for page excerpts. Supports the_excerpt Reloaded and Advanced Excerpt. Leave a comment Click here to cancel reply. Name [...]

  74. Wordpress weiterlesen Button? | mooBlogon 20 Aug 2010 at 12:52 pm

    [...] The Excert Reloaded Beispiel: » weiterlesen…’, FALSE, 2); [...]

  75. teston 26 Jan 2011 at 4:28 pm

    [...] people use plugins, such as The Excerpt Reloaded, to manage their excerpts. However, if you are happy with the core excerpt and simply want to [...]

  76. HieronymusKon 25 May 2011 at 8:26 am

    “Another issue is that when counting N words, the plugin will erase any kind of whitespace (including newlines) between each word and just create a “space”. I’m not sure if this is always an issue. It may cause paragraphs to get combined.”

    This was an issue for me. Combined paragraphs are ugly. I fixed this by changing $output = str_replace(array(“\r\n”, “\r”, “\n”, ” “), ” “, $output);

    to

    $output = str_replace(array(“\r\n”, “\r”, “\n”, ” “), “”, $output);

  77. HieronymusKon 25 May 2011 at 8:28 am

    The crucial change does not appear. Replace the chars in the array with a break return tag (<br>)

  78. My Favorite WordPress Plugins « iWEBsson 13 Oct 2011 at 2:33 pm

    [...] Reloaded - http://robsnotebook.com/the-excerpt-reloaded/&nbsp; A much better excerpt plugin for your [...]

  79. Krediteon 25 Dec 2011 at 5:12 am

    Great work! Looks good. Will implement this in my blog.

  80. [...] dies ist verstanden und der Tipp das Plugin the excerpt Reloaded zu verwenden für mich zu der Downloadseite mit den notwendigen Hinweisen. Nun geht es ans [...]

  81. [...] empfiehlt sich auf jeden Fall, das Plugin The Excerpt Reloaded zu installieren. Hierdurch eröffnen sich diverse Möglichkeiten, den Excerpt durch Parameter an [...]

  82. [...] could show the titles of five posts but include the excerpt for only the two most recent. Supports the_excerpt Reloaded and Advanced Excerpt for excerpts with HTML [...]

  83. [...] title to the post or page. Requires Page Excerpt or Excerpt Editor for page excerpts. Supports the_excerpt Reloaded and Advanced [...]

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.