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.

How to Get Your Most Recent Twitter Posts Using PHP with Caching

When we started redesigning the Pop Art blog, one of the chief requirements was to integrate everyone’s Twitter feeds into the site. In addition to the Pop Art Twitter feed in the sidebar, we wanted to add individual twitter feeds on the profile pages. The problem is that the javascript code that Twitter provides can only be called once in a single page, or it gets confused.

Since we were switching to WordPress, I checked out a bunch of Twitter plugins, but ultimately found them all to be unreliable or just missing features. In the end, I hacked together one of my own, based heavily on code by Ryan Barr. His PHP script was very nearly perfect, but I ran into three problems.

Continue reading

Dojo is Coming Together Nicely

I haven’t had a chance to have another coding marathon for Dojo yet, but I’ve been able to spend an hour or so here and there, and I’m really pleased with the way it’s coming together.

First and foremost, I’m proud of the code. I haven’t even gotten to the CSS yet, it’s just markup right now – but it’s some of the best markup I’ve ever written for a personal site. All the techniques and little touches I’ve picked up working on Sara Ryan‘s site and in the last few years at Pop Art are really paying off.

Even better than that, though, are the WordPress theme techniques I’m using. The last few versions of WordPress have added a lot of really nice bits for themers, like support for dynamic sidebars and custom widgets. Right now, I’ve got support built into the theme for several key plugins that I really like, and I’ve got three separate areas to customize with widgets.

My favorite bit, though, is the custom admin panel. I found a nice little tutorial explaining how to add one, and it’s pretty simple. As a result, I’ve added the ability to customize a couple of areas of the theme that normally aren’t editable without touching source files.

This weekend, I’m going to add some templates for a links page and an archives page, and maybe get started on some design. Miles volunteered to be a guinea pig alpha tester, so I want to get it pulled together sooner rather than later. All in all, I’m happy enough with this theme that I’m probably going to end up using a slightly modified version here on my own site.

CitySearch Categories for Richmond, Virginia

Went home for lunch today. Got a call from a recruiter 15 minutes after I got there. Did I wan to move to Richmond, Virginia, and write PHP for a young, dynamic firm?

CITYSEARCH.COM CATEGORIES FOR PORTLAND, OREGON

  • Home
  • Arts
  • Attractions
  • Bars and Nightlife
  • Hotels and Visitors
  • Movies
  • Music
  • Restaurants
  • Shopping
  • Sports & Recreation

CITYSEARCH.COM CATEGORIES FOR RICHMOND, VIRGINIA

  • Home
  • Movies
  • Local Events
  • Restaurants and Bars
  • Hotels and Visitors
  • Shopping
  • Monster Trucks

Told her no.

Besides, my Chi Kung teacher is closer to PDX, and I don’t have enough money to move right now anyway.