Hi all

I am attempting to unset the canonical tag from the head of drupal 7.
I have output the head elements in template.php using:

function mytemplatename_html_head_alter(&$head_elements) {
  print_r($head_elements);
}

which has given me the following for the canonical tag:

  [drupal_add_html_head_link:canonical:</node/1>;] => Array
        (
            [#tag] => link
            [#attributes] => Array
                (
                    [rel] => canonical
                    [href] => /node/1
                )
 
            [#attached] => Array
                (
                    [drupal_add_http_header] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Link
                                    [1] => </node/1>; rel="canonical"
                                    [2] => 1
                                )
 
                        )
 
                )
 
            [#type] => html_tag
        )

I assumed I could just use the html_head_alter hook to unset it but the following will not work:

function grocore_html_head_alter(&$head_elements) {
  unset($head_elements['drupal_add_html_head_link:canonical']);
}

Any ideas?

Thanks

Comments

dp_warren’s picture

Figured out how to unset it:

function myTemplate_html_head_alter(&$head_elements) {
  foreach ($head_elements as $key => $element) {
  	if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
            unset($head_elements[$key]);  
          } 
     }
}
dunot’s picture

For D 7.59 it's

if (isset($element['#name']) && $element['#name'] == 'canonical')
...