Skip to content

Instantly share code, notes, and snippets.

@jacine
Created August 17, 2011 19:15
Show Gist options
  • Save jacine/1152361 to your computer and use it in GitHub Desktop.
Save jacine/1152361 to your computer and use it in GitHub Desktop.
Using hook_html_head_alter() to add or modify <head> contents.
<?php
/**
* Implements hook_html_head_alter().
*
* The purpose of this alter function is to modify the contents of the $head
* variable which prints in html.tpl.php. This variable is created in
* template_process_html() using the function drupal_get_html_head(). Here we
* add 2 <meta> tags, 1 <link> tag and simplify the <meta charset />.
*/
function THEMENAME_html_head_alter(&$head_elements) {
// Simplify the meta charset declaration.
$head_elements['system_meta_content_type']['#attributes'] = array(
'charset' => 'utf-8',
);
// Force the latest IE rendering engine and Google Chrome Frame.
$head_elements['chrome_frame'] = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'),
);
// Optimize the mobile viewport.
$head_elements['viewport'] = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array('name' => 'viewport', 'content' => 'width=device-width, initial-scale=1'),
);
// Add an Apple touch icon.
$path = drupal_get_path('theme', 'THEMENAME');
$head_elements['apple_touch_icon'] = array(
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => array('rel' => 'apple-touch-icon-precomposed', 'href' => $path . '/images/apple-touch-icon.png'),
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment