How to Change the Content-Type Meta Tag in Drupal

I’m working on an HTML5 theme for Drupal 7 right now, and I needed to change the meta content-type tag. By default it looks like this: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />, and I needed the updated HTML5 version: <meta charset="utf-8" />. Normally, you can replace these things in one of the theme template files, but in this case, the meta tag was hard-coded in the Drupal source code somewhere, so I needed a programmatic solution. Here’s what I found for both Drupal 6 and 7.

Drupal 6

The meta tag is stored in the $vars['head'] array, so we can simply use the php str_replace() function to change it. Just drop this code into the template.php file for your theme.

// replace the meta content-type tag for Drupal 6
function YOURTHEME_preprocess_page(&$vars, $hook) {
  $vars['head'] = str_replace(
    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
    '<meta charset="utf-8" />',
    $vars['head']
  );
}

Drupal 7

This is slightly more complicated in Drupal 7, because instead of storing the meta tag directly in a variable, the attributes for the tag are stored in an array, which is later converted into markup. Now, we could still edit that markup, but it’s more elegant to update the attributes in the array before it’s turned into markup, rather than rely on a string replace. Here’s how to do it. Again, just drop this code into your theme’s template.php file.

// replace the meta content-type tag for Drupal 7
function YOURTHEME_html_head_alter(&$head_elements) {
  $head_elements['system_meta_content_type']['#attributes'] = array(
    'charset' => 'utf-8'
  );
}

You can use this technique to update any setting stored in that array. To see what your options are, add print_r($head_elements); inside that function. It’ll display the contents of the array on your page (though you may need to view source to make any sense of it). To edit a different tag, just replace system_meta_content_type with the item you want to alter. You can even remove an item from the array entirely like this:

// remove a tag from the head for Drupal 7
function YOURTHEME_html_head_alter(&$head_elements) {
  unset($head_elements['system_meta_generator']);
}

This is a perfect example of one of my frustrations with Drupal. For the most part, the theme system is well thought out, but if you stray from the beaten path by trying to do something they didn’t think you would need to do, you have to do it programmatically. It’s hard for me to fathom why certain parts of the markup, like this meta tag, are locked away, instead of making everything available in the theme layer. Still, this code should give you the tools you need to gain access to them.

Note: This was originally posted on my work blog, and I’m re-posting it here for archival purposes.

Version Targeting and IE8 Followup

Hooray! The feedback from the web development community convinced the IE development team to change their minds about the default setting for version targeting in IE8 (as I discussed in a previous post).

“In light of the Interoperability Principles, as well as feedback from the community, we’re choosing differently. Now, IE8 will show pages requesting ‘Standards’ mode in IE8’s Standards mode. Developers who want their pages shown using IE8’s ‘IE7 Standards mode’ will need to request that explicitly (using the http header/meta tag approach).”
– Dean Hachamovitch, Microsoft’s Interoperability Principles and IE8

To clarify, version targeting will still exist in IE8, which is a good thing. The change is that instead of defaulting to IE7′s rendering engine, it will default to IE8 — which is the behavior you would logically expect.

You know, it’s really nice to make a post where I can say something nice about Microsoft, and that’s been happening a lot more often lately, thanks to the IE development team. Way to go, guys!

Note: This was originally posted on my work blog, and I’m re-posting it here for archival purposes.

Version Targeting and IE8

Previously onWeb Developer Controversies: Aaron Gustafson from the Internet Explorer development team announced that IE8 will use a META tag to kick the engine into standards mode by targeting a specific browser version, something that was previously done by using a valid DOCTYPE. A lot of people, including Jeremy Keith, think this is a bad idea. Here are some of the more interesting points that have been raised in the discussion so far. Continue reading