Community Documentation

drupal_set_html_head

5 common.inc drupal_set_html_head($data = NULL)
6 common.inc drupal_set_html_head($data = NULL)

Add output to the head tag of the HTML page.

This function can be called as long the headers aren't sent.

▾ 6 functions call drupal_set_html_head()

chameleon_page in themes/chameleon/chameleon.theme
drupal_add_link in includes/common.inc
Add a <link> tag to the page's HEAD.
drupal_get_html_head in includes/common.inc
Retrieve output to be displayed in the head tag of the HTML page.
template_preprocess_maintenance_page in includes/theme.maintenance.inc
The variables generated here is a mirror of template_preprocess_page(). This preprocessor will run it's course when theme_maintenance_page() is invoked. It is also used in theme_install_page() and theme_update_page() to keep all the variables…
template_preprocess_page in includes/theme.inc
Process variables for page.tpl.php
_batch_progress_page_nojs in includes/batch.inc
Batch processing page without JavaScript support.

File

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

Code

<?php
function drupal_set_html_head($data = NULL) {
  static $stored_head = '';

  if (!is_null($data)) {
    $stored_head .= $data . "\n";
  }
  return $stored_head;
}
?>

Comments

Beware when using this to set

Beware when using this to set an external js file call in D6, eg:

drupal_set_html_head('<script type="text/javascript" src="http://maps.google.co.uk/maps/api/js?sensor=false" />');

If you don't include a full script closing tag (ie </script>), it will break in Firefox (and possibly other browsers, webkit-based ones seem fine). The tag will fail to close, and will wipe out any other headers up to the next available </script> tag. This usually results in all your css files failing to load, plus the next fully-tagged js file. So use this form instead:

drupal_set_html_head('<script type="text/javascript" src="http://maps.google.co.uk/maps/api/js?sensor=false"></script>');

This applies up to and including Drupal 6.16. You can overcome it in D7 with the new external option in drupal_add_js.

Changed in Drupal 7

I would like to note that this has become drupal_add_html_head() in Drupal 7.

drupal_add_html_head

Link to Drupal 7 drupal_add_html_head

Note if used in template_preprocess_page()

If you are calling drupal_set_html_head() in template_preprocess_page() you need to do this after:

$vars['head'] = drupal_get_html_head();

to make the changes have any effect in your page.tpl.php file.

Clarification & example

This means, you first make your call to drupal_set_html_head() in the THEMENAME_prepreprocess_page() function in your theme's template.php file, then add the aforementioned snippet. This is necessary so the value of the $head variable in page.tpl.php is updated from what it was originally set to in template_preprocess_page().

For example,

<?php
function mytheme_preprocess_page(&$vars, $hook) {
 
// 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();
  }
}
?>

one more little tiny change

I use the meta tags module so this code actually killed all my meta tags. To get around this I made changed the above suggestion to this:

<?php
function mytheme_preprocess_page(&$vars, $hook) {
 
// 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]" />');

   
//previous version, replacing $vars['head']
    //$vars['head'] = drupal_get_html_head();
   
    //suggested replacement, keeping old head tags
   
$vars['head'] .= drupal_get_html_head();
  }
}
?>

For now this seems to work fine. I don't really understand what I'm doing though so a word of confidence from a guru would be nice.

Simply add it to template_preprocess_page

You don't need to use this function in template_preprocess_page, you can simply add it like so.

<?php
function mytheme_preprocess_page(&$vars, $hook) { 
  
// Add Google Webmaster Tools verification to homepage.
  
if(drupal_is_front_page()) {
     
$vars['head'] .= '<meta name="google-site-verification" content="[string from https://www.google.com/webmasters/verification/verification]" />');
  }
}
?>

Placement in relation to other scripts warning

When using this from a module with hook_init(), your inserted script or whatever else you want to put into the head will be prepended to the exisitng drupal head, not appended.

Therefore, anything subsequently added to the page by other modules and such could potentially override your setting.

(E.g. if you try to set the title, or if you try to override jquery or something..)

Login or register to post comments