Google Site Verification in Drupal 7

For a freelance project I recently had to verify a Google Apps subscription. You see, Google needs to know that you actually own, or at least have full access to, the domain name you’re attempting to create an account for, and one way to “prove” this is to put a meta tag in the headers of the main page. Google rather insists (wisely) that it be the root page, so that people who might have access to subfolders for whatever reason don’t hijack things.

Anyway, I found myself faced with the daunting task of doing this in Drupal. Drupal has a lot going for it, but sometimes the simplest things are needlessly difficult. Getting control over your layout? That would be one of those things.

Let me go ahead and stop any discussion to the effect that the Metatag Module might be at all helpful here. Despite its name, it doesn’t actually let you add general metatags to your headers – only those from a custom list that it gives you. Which is fairly worthless, honestly.

Here’s what the solution turned out to be.

(1) Go to your active theme’s folder (Almost certainly sites/all/themes/{theme name}
(2) Edit template.php
(3) Drop this in it:

function {themename}_preprocess_page(&$vars, $hook) {
  if(drupal_is_front_page()) {
    $elem = array(
	'#tag' => 'meta',
	'#attributes' => array(
		'name' => 'google-site-verification',
		'content' => '{site verification code}',
	),
	
    );
    drupal_add_html_head($elem, 'google_verification_tag');
  }
}

Couldn’t be easier or more obvious, right? And to think we used to just open up layout files and add shit directly.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>