Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dp_warren on
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
Figured it out
Figured out how to unset it:
For D7.59 it's
For D 7.59 it's
if (isset($element['#name']) && $element['#name'] == 'canonical')
...