7.x common.inc drupal_add_html_head_link($attributes, $header = FALSE)

Adds a LINK tag with a distinct 'rel' attribute to the page's HEAD.

This function can be called as long the HTML header hasn't been sent, which on normal pages is up through the preprocess step of theme('html'). Adding a link will overwrite a prior link with the exact same 'rel' and 'href' attributes.


$attributes: Associative array of element attributes including 'href' and 'rel'.

$header: Optional flag to determine if a HTTP 'Link:' header should be sent.

8 calls to drupal_add_html_head_link()
drupal_add_feed in includes/common.inc
Adds a feed URL for the current page.
node_page_view in modules/node/node.module
Menu callback: Displays a single node.
openid_test_html_openid1 in modules/openid/tests/openid_test.module
Menu callback; regular HTML page with OpenID 1.0 <link> element.
openid_test_html_openid2 in modules/openid/tests/openid_test.module
Menu callback; regular HTML page with OpenID 2.0 <link> element.
taxonomy_term_page in modules/taxonomy/taxonomy.pages.inc
Menu callback; displays all nodes associated with a term.

... See full list


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


function drupal_add_html_head_link($attributes, $header = FALSE) {
  $element = array(
    '#tag' => 'link',
    '#attributes' => $attributes,
  $href = $attributes['href'];
  if ($header) {

    // Also add a HTTP header "Link:".
    $href = '<' . check_plain($attributes['href']) . '>;';
    $element['#attached']['drupal_add_http_header'][] = array(
      $href . drupal_http_header_attributes($attributes),
  drupal_add_html_head($element, 'drupal_add_html_head_link:' . $attributes['rel'] . ':' . $href);


sstedman’s picture

How do you add a weight to this? I want this to be added last.

drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => '/sites/all/themes/mytheme/images/favicons/favicon.ico', 'type' => 'image/vnd.microsoft.icon'));

sstedman’s picture

Guess I could do this...

  $shortcut_icon = array(
    '#tag' => 'link',
    '#attributes' => array(
      'rel' => 'shortcut icon',
      'href' => '/sites/all/themes/mytheme/images/favicons/favicon.ico',
      'type' => 'image/vnd.microsoft.icon',
    <strong>'#weight' => '88',</strong>
  drupal_add_html_head($shortcut_icon, 'shortcut_icon');

Would be easier within the drupal_add_html_head_link function.

iliphil’s picture

If you set $header=TRUE and you call this with a link to a page with a non ascii title you get utf8 in the http headers - in d7 there are no charset checks in the chain. It happens with translated rel="alternate"; hreflang="sv" kinds of links.