How to deliver HTML instead of XHTML with WordPress

Have you ever had the need/wish for WordPress to deliver HTML instead of XHTML in your blog? And if yes, then having no idea how to control the default XHTML tags generated in comments and its likes? Fret no more!

Insert this code at the top of your header file (that I take for granted is included in all of your web site’s pages):

// Transforming XHTML into HTML
function xml2html($buffer)
	$xml = array(' />');
	$html = array('>');
	return str_replace($xml, $html, $buffer);

This fix is taken (and just slightly modified and trimmed down for this purpose) from Tommy Olsson’s Content Negotiation post.


  • trovster says:

    I use ‘Serving XHTML with the correct mime type using PHP‘ by Neil Crosby. Works good for me (not tried it on WordPress). I only send as HTML to all, using this script.

  • Olly says:

    Ah, good, you've done the job for me. I've been putting this job off for ages πŸ™‚

  • Robert Nyman says:


    Thanks for letting people know about other options.


    I'm glad I could help! πŸ™‚

  • Sea Fraser says:

    Thank you. I can now offer a decent reply to questions I receive about fixing WordPress.

  • Robert Nyman says:


    Happy to offer you a reply.

  • I edited a few of the php files to get my WP to return valid HTML in place of XHTML, but then you have the pain of repeating the job each time an upgrade comes along… I've not bothered upgrading for while as a result, neat solution πŸ™‚

  • I'm in the same situation as Steve Williams, having manually edited all the required files and putting off to update because of it.

    Your fix seems like a good one at first site but there's a few things I'm worried about. Does the string " />" only appear in the HTML? If I post code in the page it will be changed too. Might or might not be a problem.

    The other protential problem is with with performance. All generated code has to be parsed again with this fix, something that can slow down sites with many visitors. Caching (WP-Cache) could fix that problem.

    I'll think of doing it like that, the best solution possible would be if wordpress gave me the option of selecting what code to display. I doubt that will happen though, the XHTML followers are many and very persistent πŸ™‚

  • […] the default XHTML tags generated in comments and its likes? Fret no more!. (more…)



  • Robert Nyman says:


    Yeah, me too. Therefore I'm also stuck in the "don't-dare-to-upgrade" group… 😐


    If the code appears in the post I gather that it will unfortunately be removed as well.

    I have had the same wonders about performance, but I use that exact code in this web site and I know of other highly-trafficed web sites that use the same or similar code, and they've never experienced any performance hits.

    A setting in WordPress would definitely be the best option, but like you I don't think it will happen.

  • I use more extensive filtering for every single page view, and not even being on the frontpage of Digg makes my server (shared with a lot of other sites) slow down one bit. In other words, don't worry about performance.

  • Robert Nyman says:


    Thanks for sharing. Your web site was one of the ones I knew about using filtering, but I didn't want to out you… πŸ™‚

  • Nice, I just have to use this then it seems πŸ™‚

  • Robert Nyman says:


    I don't dare to promise anything, but you should at lest give it a try… πŸ™‚

  • I can report that it works well, an update from WP 2.0.3 to WP 2.0.4 was painless with this in place. No issues whatsoever.

  • I wanted to thank you for the post. I have been sitting here with my son looking for information about this for a report he is doing for school. he goes back tomorrow and I looked everywhere! Thanks again! Have a nice night!

  • Robert Nyman says:


    Good to hear!

    Joanne ,

    Great, I'm happy it helped!

  • Bob Maloney says:

    Don't you realize that Joanne is a spambot? Her comment is generic and designed to not seem out of place on most any website.

    Go to Joanne's home page and read the text under the heading Titleist. Spam crap designed to score high for keyword titleist.

    Next time you have a conversation with a spambot, don't be so polite.

  • Robert Nyman says:


    So it seems. Unfortunately I don't have the time to check where every comment is from, so I'd rather go for the polite route to begin with. With that said, I hate spam just as much as you do.

  • BART says:

    Thanks, Robert! This is very helpful for using with ass these packages which make you serve xhtml as html (which I don't intend to do). It's also great to use against anyone who says you should use xhtml because it's more strict. It's not, you can be as strict with html4.01 as with xhtml.

    Btw: for validation, you'll have to strip the xmlns attribute from the <html> tag.

  • Robert Nyman says:


    Thanks, I'm glad that you like it! You're absolutely right about the <code>xmlns</code> attribute; it's rather that I code my templates and pages in HTML 4 to begin with, so it has never been there. Therefore, this code has only been to actually fix the rest of the content.

  • BART says:


    I decided to code in xhtml as the rest of WordPress does. This way it 'll be consistent. Thanks to your code, I can still output html, served as html.

    Thanks again.

  • RaiulBaztepo says:


    Very Interesting post! Thank you for such interesting resource!

    PS: Sorry for my bad english, I'v just started to learn this language πŸ˜‰

    See you!

    Your, Raiul Baztepo

  • Robert Nyman says:

    BART, Raiul,

    Glad it's useful to you! πŸ™‚

  • […] Robert’s Talk offers “How To Deliver HTML Instead of XHMTL with WordPress” takes content negotiation and applies it to WordPress, a technique that turns your XHTML document type to HTML. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.