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):


<?php
// Transforming XHTML into HTML
function xml2html($buffer)
{
	$xml = array(' />');
	$html = array('>');
	return str_replace($xml, $html, $buffer);
}
ob_start('xml2html');
?>

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

23 Comments/Reactions

  • #1 trovster
    September 20th, 2006 at 10:40

    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.

  • #2 Olly
    September 20th, 2006 at 11:22

    Ah, good, you’ve done the job for me. I’ve been putting this job off for ages :)

  • #3 Robert Nyman - author
    September 20th, 2006 at 16:08

    trovster,

    Thanks for letting people know about other options.

    Olly,

    I’m glad I could help! :-)

  • #4 Sea Fraser
    September 20th, 2006 at 16:37

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

  • #5 Robert Nyman - author
    September 20th, 2006 at 17:01

    Sean,

    Happy to offer you a reply.

  • #6 Steve Williams
    September 20th, 2006 at 17:33

    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 :)

  • #7 Emil Stenström
    September 20th, 2006 at 18:54

    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 :)

  • #8 Robert Nyman - author
    September 20th, 2006 at 22:42

    Steve,

    Yeah, me too. Therefore I’m also stuck in the “don’t-dare-to-upgrade” group… :-|

    Emil,

    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.

  • #9 zean.no-ip.info » How to deliver HTML instead of XHTML with WordPress
    September 20th, 2006 at 23:12

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

     

    [...]

  • #10 Roger Johansson
    September 22nd, 2006 at 19:32

    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.

  • #11 Robert Nyman - author
    September 22nd, 2006 at 20:03

    Roger,

    Thanks for sharing. Your web site was one of the ones I knew about using filtering, but I didn’t want to out you… :-)

  • #12 Emil Stenström
    September 22nd, 2006 at 23:57

    Nice, I just have to use this then it seems :)

  • #13 Robert Nyman - author
    September 25th, 2006 at 9:11

    Emil,

    I don’t dare to promise anything, but you should at lest give it a try… :-)

  • #14 Emil Stenström
    October 6th, 2006 at 21:49

    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.

  • #15 Joanne Smythington
    January 8th, 2007 at 0:46

    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!

  • #16 Robert Nyman - author
    January 8th, 2007 at 11:31

    Emil,

    Good to hear!

    Joanne ,

    Great, I’m happy it helped!

  • #17 Bob Maloney
    February 26th, 2007 at 23:48

    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.

  • #18 Robert Nyman - author
    February 27th, 2007 at 8:36

    Bob,

    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.

  • #19 BART
    February 23rd, 2008 at 21:48

    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.

  • #20 Robert Nyman - author
    February 25th, 2008 at 10:05

    BART,

    Thanks, I’m glad that you like it! You’re absolutely right about the xmlns 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.

  • #21 BART
    February 25th, 2008 at 16:45

    Robert,

    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.

  • #22 RaiulBaztepo
    March 28th, 2009 at 23:42

    Hello!
    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

  • #23 Robert Nyman - author
    March 29th, 2009 at 0:04

    BART, Raiul,

    Glad it’s useful to you! :-)

Write a comment

Twitter reactions

Share your thoughts:

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Comment preview