WordPress bug, format.php, invalid wordpress html code

It may come as a surprise to some (but not many) folk to know that there has apparently been a long running bug in wordpress from about version 2.1 which will cause your website to fail validation. It only happens in certain situations, such as when you try and add a plugin to capture form based information from your site users. When the form code is passed to the browser, a spurious ‘p’ tag gets added in to the ‘div’ tags and therefore the code is not valid… Keep in mind that the ‘doc type’ declaration is important here too, and that in my case I was using XHTML 1.0 transitional.

So what to do? Well, on the wordpress.org website there are several threads about it, including this one where I posted a response earlier today. It seems that the issue is at least ten months old, and various solutions have been speculated. Indeed, some of these solutions seem to work for some folk, but they sure as heck didn’t work for me. Largely, they involve editing one of the core wordpress files – ‘format.php’ and either adding in a new line, or taking some away. The new line is supposed to be inserted as line 67 and reads:

$pee = preg_replace( '|(</div[>]*>\s*)</p>|', "</p>$1", $pee );

This did nothing for me at all. Other solutions included commenting out lines 66 through to 68… still nothing. However, when I asked my colleague Alex Blanc to look at the code he very quickly spotted that there was a simple fix – in his words ‘a bit of a sledgehammer approach…’ but it seemed to work.

Add this as line 91:

$pee = preg_replace( '|</p></form>\s*</p></div>|', "</form></div>", $pee );

(one line only, folks, no carriage returns…)
The placing within the function in ‘format.php’ is important – put it at the bottom of the function!

Now, it may be that you don’t need this, or that it doesn’t actually work for you – it worked for me today though. The point is that this is a known issue in wordpress and has been around for nearly a whole year, and survived several updates of the software. It really ought to have been sorted out properly by now… but hey – this is open source, right? 🙂

One thought on “WordPress bug, format.php, invalid wordpress html code

Comments are closed.