Profile picture for user admin
Daniel Sipos
09 Jan 2013

This tutorial will show you how to add a new HTML tag in the HEAD part of the page in Drupal 7. You can use this to add a new meta tag, link tag or whatever you may require for your theme or module.

It is quite simple really and I will demonstrate from the perspective of a theme builder. If you have to do this for a module, just put all the code you see below in your .module file. OK, so to add a new HTML tag to the HEAD, you need to create a custom preprocess function for the html.

Open your theme’s template.php file (if your theme does not already have one, you can create it in the root folder of the theme). If you have one already, make sure there is no function name in it ending like this this: preprocess_html. If there is, you need to add this code inside that, but if there is no such function, create one (replace your_theme_name with the machine readable name of your theme, or if you do this in a module, with the name of your module):

function your_theme_name_preprocess_html(&$vars) {
 //php magic
}

For this example, I am going to add a new meta tag to the HEAD, so for this, I add the following lines into this function:

$viewport = array(
  '#tag' => 'meta', 
  '#attributes' => array(
    'name' => 'viewport', 
    'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
  ),
);

drupal_add_html_head($viewport, 'viewport');

What I just did was added a new meta tag called viewport with that specific content. In my page source, it will appear like this:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />

If you want this tag to be applied only on certain pages, look inside the $vars array and see what you can use from there for some PHP conditionals. And you can also add other HTML tags as well, like

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us

Comments

Mau 09 Jan 2013 16:54

$new_tag should be $viewport

Hello! Nice article very usefull! thank you,
But I think that in : drupal_add_html_head($viewport, 'viewport');
$viewport should be $new_tag. Or the array var name $viewport.

Dick 13 Jan 2013 19:22

Which is correct

Tried both the following but couldn't see tag in header. My theme is my own and named perfectf and I added the code to my template.php file, what am I doing wrong?

Code 1


function perfectd_preprocess_html(&$vars) {

$viewport = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
),
);

drupal_add_html_head($viewport, 'viewport');
}

Code 2


function perfectd_preprocess_html(&$vars) {

$viewport = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => '$viewport',
'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
),
);

drupal_add_html_head($viewport, 'viewport');
}

I'm going to look pretty silly once you've pointed out I've simply missed off a comma somewhere, but I couldn't get either of these to show. Can you point me in the right direction please. Thanks for your help

Daniel Sipos 14 Jan 2013 10:50

Hey there!

Hey there!

Well the second one is wrong :)

However, did you clear your cache after you saved the file with this code? I probably should have mentioned it, but whenever you add a new preprocess function into the template.php file, you need to clear the cache before drupal sees it.

Try and let me know :)

Dick 14 Jan 2013 15:34

Yes I've cleared the cache,

Yes I've cleared the cache, both within drupal and within my browser.

I've created a new template.php file that is empty apart from the following;

<?php
function Perfectd_preprocess_html(&$vars) {
$viewport = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
),
);
drupal_add_html_head($viewport, 'viewport');
}
?>

As far as I can see it's identical to your above code, but I get the following error:

Parse error: syntax error, unexpected T_STRING, expecting ')' in C:\wamp\www\sites\all\themes\Perfectd\template.php on line 13

Dick 14 Jan 2013 16:29

Sorted

My fault entirely!

The above code works perfectly.

I'd failed to heed your instruction to 'create it in the root folder of the theme and instead of adding the template.php file to my sites/all/themes/mytheme folder I'd incorrectly added it to my sites/all/themes/mytheme/templates folder.

Later I thought I had deleted it from the template folder and pasted it to the mytheme folder, but what I'd actually done was to copy it, leaving the original in place, and giving me two different template.php files and a php error.

In short:

  • It's the obvious things that trip you up
  • The original code is correct
  • The template.php file has to be in the root folder of the template.
  • Thanks for your help.

Ronit 04 Jun 2013 11:18

Drupal Novice

I've just started using Drupal. So far found it really good. Just stumbling through stuff curiously. Can you please suggest something which I should try in Drupal?

PS: The image is funny, and so apt!

Daniel Sipos 04 Jun 2013 12:18

In reply to by Ronit (not verified)

Hey there,

Hey there,

I suggest you first get the basics of site building before you go into development/theming.

Good luck! :)

City 20 Oct 2013 22:47

Module solution

Hello, I think that for adding new tags already exists module: https://drupal.org/project/opengraph_meta

I'm going to write an article about adding other meta tags also on his new site http://www.drupalcity.eu/

Audrey Gibbson 08 May 2014 12:50

Wonderful post!

Thank you for sharing this wonderful post.

keukenblad graniet 16 Nov 2014 08:49

This information is very useful for me, thank you very much

This information is very useful for me, thank you very much

nd 21 May 2015 23:34

does this work for drupal 6 version?

does it work on drupal 6 version..I've cleared cache and everything and I still don't see it.

thanks

Alex 30 Sep 2015 08:53

Problem - blank page

Hi, i have tried to put the code from above as it was described, and it gives me a blank page for the entire site :( Some Help will be really appreciated. Thx.

adil sarwar 28 Jul 2016 14:06

Very helpful..

hi dear...this article is very helpfull to me..thanks...i have one question.

if i want to add meta description then what i have to do...

Tibor 17 Nov 2018 20:45

Very helpful

Hey there! I'm trying to add meta tags for a website for a friend in Drupal 8. Your two articles sound were very helpful to me – what I'm unclear about is where i actually have to enter this (in order to add it for the entire website). Thanks a lot in advance!

Add new comment