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?
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.