WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I noticed that the default WP theme uses force_balance_tags for listing posts' excerpts. This way, it doesn't cause major html breakage whenever someone writes a post like

<div><b>This is an excerpt. <!--more--> and this is more text... </b></div>

It will close tags instead of bolding everything, and breaking div.

Why doesn't the normal wordpress page do this by default, in the default theme? That way, broken page content like:

</div></div> now text is not where it's supposed to be </div> unmatched tags

would not cause unexpected behavior.

Was this an oversight, or are there certain things that break when using force_balance_tags on pages?

share|improve this question
up vote 6 down vote accepted

force_balance_tags() is not a really safe function. It doesn’t use an HTML parser but a bunch of potentially expensive regular expressions. You should use it only if you control the length of the excerpt too. Otherwise you could run into memory issues or some obscure bugs.

The other problem is: the function uses two hard-coded lists of elements for single tags and nestable tags. There are multiple places where WordPress uses such lists, and they are not kept in sync. So when you use an element that isn’t part of these lists or changed its nesting behavior you may end up with incorrect markup.

share|improve this answer
Here is a more robust implementation of force_balance_tags using DOMDocument: gist.github.com/westonruter/4b1fca6f20fbc95c3c34 – Weston Ruter Mar 2 '15 at 5:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.