Community Documentation

HowTo: Add Google Webmasters Tools verification meta tag via the themes template.php file.

Last updated December 14, 2012. Created by tecjam on January 20, 2012.
Log in to edit this page.

This guide refers to the meta tag verification method available for Google Webmaster Tools and these instructions are specific for Drupal 7.

You can also add any other meta tag you require using this guide.

edit: Since writing this guide a module has popped up that does all this for you. You can find the Site verify module here. However, if you require a more complex approach - see the first comment for an example - or just want to learn a bit more about how it works or simply prefer the DIY approach then please do read on:

What you require:

  • Google Webmasters Tools Account - sign up here: https://www.google.com/webmasters/tools/
  • Custom theme (or a copy of a supplied theme) in either /sites/default/themes/ or /sites/all/themes/
  • Google Webmaster Tools Verification Code (meta tag)

1) Make sure you have your Google Webmaster Tools Verification Code meta tag. It looks something like this:
<meta name="google-site-verification" content="abcdefghijklmnopqrstuvwxyz0123456789" />

2) Browse to your themes directory. If you plan to modify your theme, it is best practice to either create a new custom theme, or copy an existing theme (or create a sub-theme) in your sites/default/themes/ folder. If you don't know what I mean, you ought to read up on custom themes for Drupal 7 before continuing with this guide.

3) In your themes directory, open the template.php file in an editor. If you don' t not have such a file, create a blank page and call it template.php.


4) To add the meta tag we will need to use template_preprocess_html.

Initially I thought it would be possible by using the hook_html_head_alter but this will only allow already defined $head_elements to be modified or unset, so we need to first create the element and then add it using drupal_add_html_head.

Check your template.php file to see if you already have a YOUR_THEME_NAME_preprocess_html function defined. If yes, you will need to paste the code inside the function into it.
Make sure your template.php file beginns with a <?php tag if you have created a new blank file.

5) Paste the function into the template.php file. Copy the code below and paste it into your template.php file:

/**
* 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');
}

6) CHANGE THE FUNCTION NAME 'YOUR_THEME_NAME' TO YOUR THEME NAME eg: garland_preprocess_html


7) EDIT THE CODE

Check this line of the code and replace the code with the one google supplied you with when you added your site to the Google Webmasters Tools.

      // REPLACE THIS CODE WITH THE ONE GOOGLE SUPPLIED YOU WITH
      'content' => 'abcdefghijklmnopqrstuvwxyz0123456789',


8) Save your file and clear all cache.

9) Reload your site and check your source code to check if your meta tag has been added to the header.

10) Done!

I hope this has helped someone with the same problem as me.

Comments

Only show this tag on a specific node?

Can I only show this tag on a specific node? For instance, what if I wanted this meta tag to only show on the home (front) page? What if I wanted it to show on node 25?

Thanks!

Harold Cabalic
Web Developer

One way would be this

FOR THE FRONT PAGE:
For the front page check you don't need to load the node object, so you can simply check for the front page before you call drupal_add_html_head() function in your hook_preprocess_html function.

if(drupal_is_front_page()) { drupal_add_html_head(...) }

FOR SPECIFIC NODE ID ETC ..
You need to load the node object from the arg(1), so you would simply do the following in the themehook_preprocess_html function:

  // load the node
  $node = node_load(arg(1));

Then you do it the following way ...

/**
* 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',
    )
  );

  // load the node
  $node = node_load(arg(1));

  if($node->nid == '25') {
    // Add Google Webmasters Verification Meta Tag to head
    drupal_add_html_head($google_webmasters_verification, 'google_webmasters_verification');
  }

}

Of course since you have the entire node object you could do various other checks as well, eg: content type or check specific fields .. and so forth ..

Verify Me

Orrrrr, there's the Site Verify module which will do this all for you.

--Andy
Developing Drupal websites for Livelink New Media

Yup

Shame there's no docs with that module.

Thanks for the link to the

Thanks for the link to the module, kingandy. I've added it to the page above.

@ NikLP: You can help write the documentation for the module. ;-)

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Level
Beginner
Audience
Designers/themers
Keywords
google, template, theme, webmaster

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here