function drupal_add_html_head

You are here

7 common.inc drupal_add_html_head($data = NULL, $key = NULL)
8 common.inc drupal_add_html_head($data = NULL, $key = NULL)

Adds output to the HEAD tag of the HTML page.

This function can be called as long as the headers aren't sent. Pass no arguments (or NULL for both) to retrieve the currently stored elements.

Parameters

$data: A renderable array. If the '#type' key is not set then 'html_tag' will be added as the default '#type'.

$key: A unique string key to allow implementations of hook_html_head_alter() to identify the element in $data. Required if $data is not NULL.

Return value

An array of all stored HEAD elements.

See also

theme_html_tag()

8 calls to drupal_add_html_head()
drupal_add_html_head_link in includes/common.inc
Adds a LINK tag with a distinct 'rel' attribute to the page's HEAD.
drupal_get_html_head in includes/common.inc
Retrieves output to be displayed in the HEAD tag of the HTML page.
install_display_output in includes/install.core.inc
Displays themed installer output and ends the page request.
openid_test_yadis_http_equiv in modules/openid/tests/openid_test.module
Menu callback; regular HTML page with <meta> element.
rdf_preprocess_node in modules/rdf/rdf.module
Implements MODULE_preprocess_HOOK().

... See full list

File

includes/common.inc, line 299
Common functions that many Drupal modules will need to reference.

Code

function drupal_add_html_head($data = NULL, $key = NULL) {
  $stored_head = &drupal_static(__FUNCTION__);

  if (!isset($stored_head)) {
    // Make sure the defaults, including Content-Type, come first.
    $stored_head = _drupal_default_html_head();
  }

  if (isset($data) && isset($key)) {
    if (!isset($data['#type'])) {
      $data['#type'] = 'html_tag';
    }
    $stored_head[$key] = $data;
  }
  return $stored_head;
}

Comments

An example, for those who are a bit confused as to the change from Drupal 6 to 7. Here's how I added the <link> attribute for Google's Font API inclusion to a Drupal 7 site:

<?php
// First, we must set up an array
$element = array(
 
'#tag' => 'link', // The #tag is the html tag - <link />
 
'#attributes' => array( // Set up an array of attributes inside the tag
   
'href' => 'http://fonts.googleapis.com/css?family=Cardo&subset=latin',
   
'rel' => 'stylesheet',
   
'type' => 'text/css',
  ),
);
drupal_add_html_head($element, 'google_font_cardo');
?>

This will output the following HTML:

<link href="http://fonts.googleapis.com/css?family=Cardo&amp;subset=latin" rel="stylesheet" type="text/css" />

If you're adding link tags, you can also use drupal_add_html_head_link().

you can use the markup type. For example, adding some jquery tmpl templates:

<?php
    $inline_script
= <<<EOL
      <script id="tp1" type="text/x-jquery-tmpl"><li>\${Name}</li></script>
      <script id="tp2" type="text/x-jquery-tmpl"><li>\${date}</li></script>
EOL;
   
$element = array(
     
'#type' => 'markup',
     
'#markup' => $inline_script,
    );
   
drupal_add_html_head($element, 'jquery-tmpl');
?>

Here is an example for adding the IE meta tag to force the rendering engine with some chrome frame love thrown in:

Goal is to get this meta tag in the header of your theme:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

Use the theme_preprocess_html hook, so add this snippet to your template.php file:

/**
* Preprocesses the wrapping HTML.
*
* @param array &$variables
*   Template variables.
*/
function YOUR_THEME_NAME_preprocess_html(&$vars) {
  // Setup IE meta tag to force IE rendering mode
  $meta_ie_render_engine = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'content' =>  'IE=edge,chrome=1',
      'http-equiv' => 'X-UA-Compatible',
    )
  );
 
  // Add header meta tag for IE to head
  drupal_add_html_head($meta_ie_render_engine, 'meta_ie_render_engine');
}

In some cases IE will ignore this rendering mode directive unless it's the first tag to appear after the opening head tag. This can be achieved by specifying a very low weight property as follows:

$meta_ie_render_engine = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'http-equiv' => 'X-UA-Compatible',
      'content' =>  'IE=edge,chrome=1',
    ),
    '#weight' => '-99999',
  );

A quick way for adding a specific opengraph image from your nodes is by adding this to the relevant node.tpl.php.

<?php
$imgpath
= image_style_url('YOUR_STYLE', $YOUR_FIELD[0]['uri']);
$element = array(
 
'#tag' => 'meta',
 
'#attributes' => array(
   
'property' => 'og:image',
   
'content' => $imgpath,
  ),
);
drupal_add_html_head($element, 'og_image');
?>

Can you do this with conditional HTML? How would I go about getting this into my head?

<!--[if (gte IE 6)&(lte IE 8)]>
  <script type="text/javascript" src="selectivizr.js"></script>
  <noscript><link rel="stylesheet" href="[fallback css]" /></noscript>
<![endif]-->

@danbohea

This is how I did it using MYTHEME_preprocess_html in template.php:

  $path = $GLOBALS['base_url'] . '/' . path_to_theme() . '/js/selectivizr-min.js';
  $selectivizr = array(
    '#tag' => 'script',
    '#attributes' => array(
      'src' => $path,
    ),
    '#prefix' => '<!--[if lte IE 8]>',
    '#suffix' => '</script><![endif]-->',
  );
  drupal_add_html_head($selectivizr, 'selectivizr');

Hello, can drupal_add_html_head be used within a module (od even a computed field), or does it need to be used inside a template?

I need to put a tag if a CCK field is "private" and I need to do it in head.. I know how to handle the logic but I would prefer using this function within a module or a computed_filed, as I don't already have overriden templates.
Would it be possible ?

of course you can and you have to.

I don't like this new format for drupal 7 because script tag don't add the closer tag and occur errors.

I will use the inline method, thank you @mtsanford

Oh i didn't see the '#suffix' => '' option

$data = array(
    '#tag' => 'meta',
    '#attributes' => array(
       'name' => 'description',
       'content' => 'Enter your meta description here.',
    ),
  );
  drupal_add_html_head($data, 'sales_floor_home');

This also works as it is only for the head tag.

function YOUR_THEME_NAME_html_head_alter(&$head_elements) {
  $head_elements['system_meta_ie_browser'] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'http-equiv' => 'X-UA-Compatible',
      'content' => 'IE=edge,chrome=1',
    ),
    '#weight' => -99999,
  );
  drupal_add_html_head($head_elements, 'system_meta_ie_browser');
}

I am using it in the same funcion hook_menu is calling for a search result page.

<?php
$html_head
= array(
   
'browser_cache' => array(
     
'#tag' => 'meta',
     
'#attributes' => array(
       
'content' => 'no-cache, no-store, must-revalidate',
       
'http-equiv' => 'Cache-Control'
     
)
    ),
   
'browser_pragma' => array(
     
'#tag' => 'meta',
     
'#attributes' => array(
       
'content' => 'no-cache',
       
'http-equiv' => 'Pragma'
     
)
    ),
   
'browser_expires' => array(
     
'#tag' => 'meta',
     
'#attributes' => array(
       
'content' => '0',
       
'http-equiv' => 'Expires'
     
)
    ),
  );
  foreach (
$html_head as $key => $data) {
   
drupal_add_html_head($data, $key);
  }
?>

HTML meta tags are not working in all cases (nearly none). Better use drupal_add_http_header, if you like to use both you might add

<?php
drupal_add_http_header
($data['#attributes']['http-equiv'],$data['#attributes']['content']);
?>

into foreach

Without editing Drupal core file, I want to add a new tag (e.g. ) before the existing tag (e.g.