4

I try to remove shortlink and generator link from my site.

<meta name="generator" content="Drupal 7 (http://drupal.org)">
<link rel="shortlink" href="http://landberg.at/nl/node/109">

I used below code in template.php but when i look in my source code its still there?

function landberg_html_head_alter(&$head_elements) {
    unset($head_elements['system_meta_generator']);
    foreach ($head_elements as $key => $element) {
        if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
          unset($head_elements[$key]);
        }
        if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'shortlink') {
          unset($head_elements[$key]);
        }
    }
}

What is best way to remove this?

5
  • Your code is correct. Make sure that you cleared the cache. Feb 25, 2016 at 12:24
  • @Aram Boyajyan i already cleared cash but the code is still showing up?
    – meez
    Feb 25, 2016 at 13:38
  • Strange; I checked the code and it works fine. Is the name of your theme exactly landberg? Add there a watchdog entry (e.g. watchdog('debug', 'hook_html_head_alter() called from the theme');) to make sure it is executed. Feb 25, 2016 at 13:58
  • it was because it was overwritten by Meta tag module... Thanks
    – meez
    Feb 25, 2016 at 14:14
  • Good catch - glad you managed to resolve it! Feb 25, 2016 at 14:15

2 Answers 2

4

Above code is good but it didn't work because it was overwritten by Meta tag module. I adjust some settings in Configuration » Search and metadata and now generator tag and shortlink is gone.

2
  • That is a proper solution. Sadly it needs to be done on all Metatag elements.
    – kwoxer
    Jun 4, 2018 at 15:08
  • You could also simply add unset($head_elements['metatag_shortlink']); to your above snippet. Although I don't like how this then invalidates the admin UI. As no matter what anyone will put in these fields on the Metatag settings form, the values will be overwritten by that line.
    – leymannx
    Nov 23, 2018 at 22:15
0

You can utilize https://www.drupal.org/project/disable_link_rel, as it's specifically designed for that purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.