Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 4 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

Your Answer

 
discard

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.