Adding custom code to <head> in Drupal

AKor picture AKor · Feb 14, 2011 · Viewed 20.6k times · Source

I'm trying to figure out where the <head> for all of the pages in Drupal is (I'm using Orange theme if it matters). I have to add analytics code into the <head>.

Inside which file would I find the <head>?

Answer

jpstrikesback picture jpstrikesback · Feb 14, 2011

Use drupal_set_html_head() by placing this in your themes template.php file. If the function MYTHEMENAME_preprocess_page() already exists insert what is inside the {closure} brackets below (before the $vars['head'] if that exists in it as well) :

function MYTHEMENAME_preprocess_page(&$vars, $hook) {
  // say you wanted to add Google Webmaster Tools verification to homepage.
  if (drupal_is_front_page()) {
    drupal_set_html_head('<meta name="google-site-verification" content="[string from https://www.google.com/webmasters/verification/verification]" />');
    $vars['head'] = drupal_get_html_head();
  }
}