Adding OpenId Delegation to Drupal 7 site.

Further to reading about getting nice OpenId for your site you might wonder "How do I add those two lines to my Drupal site <head> section?" Forget about installing additional modules - they have very little chance of being supported in the next Drupal release. Instead, read on and see how easy it is to do this yourself. I will use Drupal 7 for this example (because this site is built upon Drupal 7 at the time), but you should be able to adapt the code to suit other Drupal versions with few minutes of googling for alternative functions.

First of all, find template.php file for your theme. The good practice is to create a sub-theme rather than editing standard theme. This is because any update to standard theme will nullify your changes and you'll have to do them all over again. Sub-theming is widely covered elsewhere, I will just assume that you're doing this the right way. Let's open template.php for your (hopefully sub-) theme and look for

function YOUR_THEME_NAME_preprocess_html(&$variables, $hook) {

function. preprocess_html function is where you can edit $head variable before it's used elsewhere (most notably in html.tpl.php where it gets into <head> section of your site). Drupal 7 introduced drupal_add_html_head() function which is the correct way to add to $head variable from now on. This site uses myopenid.com as OpenId provider (its server address is "http://www.myopenid.com/server") and the OpenId it's using is "http://docfish-ru.myopenid.com/", so we need to put the following lines into <head> section:

<head>
  ...
  <link rel="openid.server" href="http://www.myopenid.com/server" />
  <link rel="openid.delegate" href="http://docfish-ru.myopenid.com/" />
</head>

Here's how:

function YOUR_THEME_NAME_preprocess_html(&$variables, $hook) {
  if(drupal_is_front_page()) { //use instead of $is_front variable not available in template.php
    //Set up an array for adding to HEAD section
    $element_openid_server = array(
      '#tag' => 'link', //HTML tag - <link/>
      '#attributes' => array( //Attributes inside <link/> tag
        'rel' => 'openid.server',
        'href' => 'http://www.myopenid.com/server',
      ),
    );
    $element_openid_delegate = array(
      '#tag' => 'link',
      '#attributes' => array(
        'rel' => 'openid.delegate',
        'href' => 'http://docfish-ru.myopenid.com/',
      ),
    );
    
    drupal_add_html_head($element_openid_server, 'openid_server');
    drupal_add_html_head($element_openid_delegate, 'openid_delegate');
  }
}

Note usage of drupal_is_front_page() function - you wouldn't want to add OpenId tags anywhere but front page, it'll be a waste of bandwidth. Save and upload template.php to your site. Don't forget to rebuild your theme registry for changes to apply.

Rating: