I have a bartik subtheme in Drupal 7. I can't figure out how to add meta tags to the head. It was easy to do in D6. Is there any way to to this in D7?

Comments

traceelements’s picture

I've been trying to read Drupal 7 api, but unfortunately, I don't have php experience. Do I need to add a function to my custom template.tpl.php to print the link to the head? Can anyone help?

I see that html.tpl.php has this code:

<?php print $head; ?>

So how do I add something (in my case, meta tags) to the $head?

traceelements’s picture

Anyone?

betarobot’s picture

If you don't have php experience the best way would be to use module like http://drupal.org/project/nodewords http://drupal.org/project/metatags. But the problem is none of them is D7 ready yet.

There is at list a temporary solution for now: http://drupal.org/project/metatags_quick

traceelements’s picture

Thanks, Andrey. I did see that module, but I don't want different meta keywords and descriptions for each page on the website. I really just want to put in a simple meta keywords and description in the head for the whole website. It just seems crazy that I can't do that without knowing php.

traceelements’s picture

There's probably a way to run a function in my template.tpl.php, but I couldn't figure out how to do it.

The solution that I found, for now, is this:

Copy html.tpl.php from modules/system to my custom theme templates folder. Then add my meta keywords and description code into the head.

Sakrecoer’s picture

I must say i'm curious about this too.... actualy your bypass trick didn't work for me.

open your mind open the source
express yourself, share your tools

AmericasThirdParty’s picture

OK, without php:
Open up modules/system/html.tpl.php
I use Notepad++ to Edit
under the following line:

print $head_title;

After the end title tag, put your meta information.
Hope this helps!

duckzland’s picture

can you use hook_preprocess_html and modify the variables there instead?

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

jpfeifer’s picture

Yes, I just noticed how difficult Drupal 7 seems to have made something that would have been so easy to do just knowing HTML. I just want to have access to my HTML code in the template file so I can place IE conditional comments in there, but that code seems to have been abstracted from the templates and comes from the database now?

And the suggestions to change it are to install extra modules?

Personally, I'd prefer the old method of being able to change the HTML, and customizing the $head variable instead to output.

What might be the rationale for having done it the other way? Now it makes simply adding an IE conditional comment cost a few hours of googling versus 10 seconds copy / paste.

duckzland’s picture

that is the "drupal way", you can do the html copy paste by copying html.tpl.php to your theme directory and edit the html for tag there

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

Diane Bryan’s picture

I put second version of html.tpl.php into the /[subtheme]/templates/ directory. (where [subtheme] is replaced by the subtheme that is enabled for my site--the place where all my customization is done)

In the customized html.tpl.php in that directory, I paste the code for the meta tag before the closing head tag (careful not to interfere with any php calls).

Then following the trusty-dusty "Clear Caches when altering templates" rule:
Administration » Configuration » Development

I refresh my page. Voila! If you do Control+U your new code should be there on the page, just above the end of the head tag.

REMEMBER: If you update your subtheme and for some reason it is not a sub-subtheme, you will likely have to re-introduce any custom code. For this reason, step zero should always be to make sure you are working on a sub to a theme or sub-sub to a base theme.

jezzaYo’s picture

Very old post, but showed up first when searching so thought id stick a link to a good answer! - https://www.drupal.org/node/1411428

Add this to your template.php

/**
* Preprocesses the wrapping HTML.
*
* @param array &$variables
*   Template variables.
*/
function YOUR_THEME_NAME_preprocess_html(&$vars) {
  
  // Setup Google Webmasters Verification Meta Tag
  $google_webmasters_verification = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'google-site-verification',
      // REPLACE THIS CODE WITH THE ONE GOOGLE SUPPLIED YOU WITH
      'content' => 'abcdefghijklmnopqrstuvwxyz0123456789',
    )
  );
  
  // Add Google Webmasters Verification Meta Tag to head
  drupal_add_html_head($google_webmasters_verification, 'google_webmasters_verification');
}
vinoth.babu’s picture

This works as it is only for the head tag.

function YOUR_THEME_NAME_html_head_alter(&$head_elements) {
$head_elements['system_meta_ie_browser'] = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'http-equiv' => 'X-UA-Compatible',
'content' => 'IE=edge,chrome=1',
),
'#weight' => -99999,
);
drupal_add_html_head($head_elements, 'system_meta_ie_browser');
}

kapildoshi’s picture

I may be wrong in this but you can try using metatag module to add metatags in header.