Support » Themes and Templates » Serving up “application/xhtml xml”

  • If the following bit of code, by Neil Crosby, is used instead of <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
    <html xmlns="" xml:lang="en">
    then your pages will serve up as application/xhtml+xml to browsers woman enough to understand it, and html/text to Internet Explorer. It also has the bonus of telling the W3C validator that it is xml. Whilst it is the kind of thing I would like to see built into WordPress I realise that serving up web pages as XML isn’t everyone’s cup of tea, invalid=won’t load.
    I am useless with PHP unless the task is cutting and pasting or simpler, but I thought that someone else might well find this useful or have an idea how to crowbar it into WordPress with greater aplomb.
    $charset = "utf-8";
    $mime = "text/html";
    function fix_code($buffer) {
    return (str_replace(" />", ">", $buffer));
    if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
    # if there's a Q value for "application/xhtml+xml" then also
    # retrieve the Q value for "text/html"
    $_SERVER["HTTP_ACCEPT"], $matches)) {
    $xhtml_q = $matches[1];
    $_SERVER["HTTP_ACCEPT"], $matches)) {
    $html_q = $matches[1];
    # if the Q value for XHTML is greater than or equal to that
    # for HTML then use the "application/xhtml+xml" mimetype
    if($xhtml_q >= $html_q) {
    $mime = "application/xhtml+xml";
    # if there was no Q value, then just use the
    # "application/xhtml+xml" mimetype
    } else {
    $mime = "application/xhtml+xml";
    # special check for the W3C_Validator
    if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
    $mime = "application/xhtml+xml";
    # set the prolog_type according to the mime type which was determined
    if($mime == "application/xhtml+xml") {
    $prolog_type = "<?xml version='1.0' encoding='$charset' ?>
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN'
    <html xmlns='' xml:lang='en'>";
    } else {
    $prolog_type = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
    <html lang='en'>";
    # finally, output the mime type and prolog type
    header("Content-Type: $mime;charset=$charset");
    header("Vary: Accept");
    print $prolog_type;

Viewing 7 replies - 1 through 7 (of 7 total)
  • Best proceed with care with this.

    I was wondering if anyone could tell me how to put code like this into a plug in?

    I was going to hack the original WordPress file (I think it’s in wp-header.php), but decided I’d rather be able to release it to the public.

    Any help on how to override the default Content-type? It seems as though it fetches it from the database using the Get_option() function. I don’t want to be setting the database everytime someone with a different browser turns up, either.

    Is there an easier way?

    have a look at this, it works for me (wp 1.5).
    Some of the filter hooks are not documented, I discovered this one by digging through the source files 😉

    strange, I can’t edit my previous post… I changed the name and url of the plugin (for a better view on Google), so now it’s called content-negotiation for wordpress

    xhtml (strict) pays no attention to the MIME type. It is implied in the DTD.




    There’s also this plugin:

    Sorry to bump such an old thread, but I have a question before I implement this. What happens if one of my commenters posts something with invalid code? WP is supposed to correct this stuff, but it isn’t infallible. When I did a dry run locally I immediately found that a couple of pages wouldn’t parse because of a invalid code in comments.

    How can I protect my blog from this threat, if I am to use xhtml+xml?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Serving up “application/xhtml xml”’ is closed to new replies.