Patch: Balancing pre-‘more’ tags

One of my nits about WP, from my original formatting problems post, was that despite balancing tags for an entire post, WP wouldn’t do so for text appearing before a <!–more–> tag. Others have noted this in the support forums, and it was reported as bug 0000178, titled “WP doesn’t close tags when breaking a page with <!–more–>”. This fix is actually quite simple.

In wp-includes/templates-functions-post.php, get_the_content() is the function responsible for retrieving post data from the database and appropriately parsing it according to the current situation (i.e. showing just pre-‘more’, or whole post, or particular page of a post, etc).

In get_the_content(), I changed this:

if (count($content)>1) { if ($more) { $output .= '<a id="more-'.$id.'"></a>'.$content[1]; } else { $output .= ' <a href="'. get_permalink() . "#more-$id">$more_link_text</a>"; } }

to this:

if (count($content)>1) { if ($more) { $output .= '<a id="more-'.$id.'"></a>'.$content[1]; } else { $output = balanceTags($output); $output .= ' <a href="'. get_permalink() . "#more-$id">$more_link_text</a>"; } }

I just added the line:

$output = balanceTags($output);

This should be a safe call to make because balanceTags() will fix whatever tags appeared before the <!–more–>, only doing so when the pre-‘more’ text is being returned. Keep in mind that balanceTags() will return with whatever text was sent to it if the setting ‘use_balanceTags’ is turned off, so only someone who requested that WP balance tags for them will see this benefit. The text in the database is unaffected by this change, and as well, the retrieval of the whole post will not be affected since the tags need to balance the pre-‘more’ text won’t be inserted during such a call.

Here’s the patch (modified from, and then compared against, the latest WP 1.3 from CVS).

One thought on “Patch: Balancing pre-‘more’ tags

Comments are closed.