How can I get the WordPress language_attributes function to return valid XHTML 1.1?


I have a WordPress template that contains the following element:

<html xmlns="" <?php language_attributes('xhtml'); ?>>

This returns:

<html xmlns="" dir="ltr" lang="en-US" xml:lang="en-US">

Unfortunately the "lang" attribute is invalid XHTML 1.1 - and the client would like this level of validation.

WordPress' general-template.php file contains the following code:

if ( get_option('html_type') == 'text/html' || $doctype == 'html' )
    $attributes[] = "lang=\"$lang\"";

$doctype is the parameter passed to it (in this case 'xhtml'). Should get_option be returning a value other than 'text/html'? If so, what should I be setting in WordPress to achieve this - if anything?

I've also tried using preg_replace to take out the "lang" attribute, but this didn't seem to be able to match the text. If I enter the text manually, it matches! Possibly an encoding issue with the string being returned by language_attributes?

8/7/2012 6:40:18 PM

Accepted Answer

I solved this. There's a "language_attributes" filter, so I wrote a plugin that hooks into that and does a simple preg_replace. The replace worked when performed here, and it's a pretty neat way to handle it.


As requested, here's the code I used:

Plugin Name: Create Valid XHTML 1.1
Plugin URI:
Description: Removes deprecated "lang" attribute from (X)HTML header.
Author: dommer
Version: 1.0.0
Author URI:

function create_valid_xhtml_1_1($language_attributes) 
    return preg_replace('/ lang=\"[a-z]+\-[A-Z]+\"/', '', $language_attributes);

add_filter('language_attributes', 'create_valid_xhtml_1_1');
12/21/2009 11:09:38 AM

If this is just a theme on your own site, you could edit header.php and change the

<html xmlns="" <?php language_attributes('xhtml'); ?>>

line to be hardcoded, improves performance too :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow