July 28, 2011

Google+ Profile photo in search results for your Drupal blog

Recently, Google started displaying Google Profile pictures beside its search results. This article will show you how you can set up your Drupal blog / site so that your articles are correctly linked to your Google Profile and (hopefully) your profile photo appears in Google's search results.

Google+ Profile picture in search results

There is one caveat before we get started that you should be aware of: At the moment, Google is only extending this functionality to certain outlets, so you might not actually see any change in your search results for awhile. Think of this then as future-proofing rather than an immediate new feature for your site.

To identify your blog posts as yours, and then link your Google+ profile to those posts, we'll go through Google's recommendations and then discuss how to perform each step on your Drupal site. As described in the recommendations from Google, we'll need to address the following:

  1. Each blog article should link to your author page on the same site using rel="author", like this example:

    <a rel="author" href="http://www.millwardesque.com/blogs/christopher-millward/">Christopher Millward</a>

    Within Drupal, you can perform this change either in a theme or a module, so feel free to work with whatever makes the most sense for your type of installation. For this post, I'll cover how to make the change at the theme level. If you'd like an example on how to use a custom module instead, leave a comment and I'll update this post.

    To add the rel="author" tag into your articles, we're going to update the link to the author's blog (displayed at the bottom of every blog node page, usually pointing to http://www.example.com/blogs/username). In order to change this link at the theme level, you'll need to add or modify your theme's implementation of theme_preprocess_node in template.php to detect the blog link and set the "rel" attribute

    function mytheme_preprocess_node(&$vars) {
        if ($vars['node']->links['blog_usernames_blog']) {  // Only edit nodes that have the blog username links set
        $vars['node']->links['blog_usernames_blog']['attributes']['rel'] = 'author';  // Set the "rel" attribute to "author"
        // Rebuild the themed links with our new "rel" tag, since they'd been themed and set before this function was called
        $vars['links'] = theme('links', $vars['node']->links, array('class' => 'links inline'));
  2. Your author page should link to your Google Profile using rel="me", like this example:
    <a rel="me" href="https://plus.google.com/105240469625818678725/"><img src="//www.google.com/images/icons/ui/gprofile_button-16.png"> </a>

    This one you can approach in a number of different ways. You could copy and paste the sample code above into a block, and set the block to appear only only your author page (replacing the Google+ URL with a link to your own Google+ profile). Alternately, you can edit page.tpl.php to include this link if you want it on every page

  3. Your Google Profile should link back to your author page on the website. When you add this link to your profile, be sure to check the "This page is specifically about me" checkbox. This creates a rel="me" link to that author page.
    This isn't Drupal-specific, but it took me longer than I care to admin to find the aforementioned "Links" section, so I'll mention it here:
    1. Visit the "About" page on your Google+ Profile
    2. Click the "Edit Profile" button
    3. The "Links" section is in the right-hand side of the page. Add a link to the blog author page as described in the instructions from Google.
  4. Test that your site is properly configured and linked. Test a few of your article URLs at Google's Rich Snippets Webmaster Tool and ensure that the text "Verified: Authorship markup is correct for this page" appears in the "Extracted Author for this page" section
  5. Fill out Authorship request form. I've not been able to tell if this is required to complete the process, but it'll keep you notified of any issues or platform changes that may occur so it's a good idea regardless.

And that's that! Like I said at the top, results probably won't be immediate unless you've a high-profile blog, but you can rest assured that your profile image will appear as soon as Google allows it.