SEO, Meta Tags module and <title> tags

If you're one of those SEO obsessed people and if you're also a Drupal user, then you'll probably know about the Meta Tags module (also called Nodewords). You might even know about the Page Title module too. And, just maybe, you've also tried to find a way to change the <title> text, not only on nodes (which Page Title does just fine), but also on taxonomy/term pages (which neither module does at all).

I had a client who needed to change the text on term pages for better SEO. Now, he didn't want crazy long term names, so we couldn't do it that way, and neither of the two modules above could do it. So how to do it?

The only solution I could come up with was to hack one of the two modules, and seeing as Meta Tags managed to put an option on every node edit page AND on every term edit page, that was the logical starting point.

So after a bit of thinking, searching and hacking around, I came up with a workable solution. OK, so its not pretty, and you have to do one extra step, but it works!

First, you have to patch the nodewords.module file, and tell it about the Title setting. You also need a title.inc file so that Nodewords knows how to handle the Title field. And lastly, you need to add some code into template.php to change the page title.

This is the patch (exclude the php tags):

<?php
@@ -686,6 +686,7 @@ function _nodewords_get_settings($defaul
      
'geourl' => 1,
      
'keywords' => 1,
      
'robots' => 1,
+     
'title' => 0,
     ),
    
'edit' => array(
      
'abstract' => 0,
@@ -
694,6 +695,7 @@ function _nodewords_get_settings($defaul
      
'geourl' => 0,
      
'keywords' => 1,
      
'robots' => 0,
+     
'title' => 0,
     )
   );

@@ -782,7 +784,12 @@ function _nodewords_prepare($type, $ids,
    
// so unfortunately, only node-pages can get a DC.title.
    
if ($type == 'node' && count($ids) == 1) {
      
$node = node_load($ids[0]);
-     
$tags['DC.title'] = $node->title;
+      if (
$tags['title']) {
+       
$tags['DC.title'] = $tags['title'];
+      }
+      else {
+       
$tags['DC.title'] = $node->title;
+      }
     }
     elseif (
$type == 'panels' && count($ids) == 1) {
       if (
function_exists('panels_api_version') && module_exists('panels_page')) {
?>

And this is the code for template.php (exclude the php tags):

<?php
function _phptemplate_variables($hook, $vars) {
  if (
$hook == 'page') {
   
// check if Meta Tags module (nodewords) is loaded
   
if (module_exists('nodewords')) {
     
$tags = nodewords_get();
     
// if the title metatag is set, use it as the HTML page title
     
if ($tags['title']) {
       
// set the page title to the meta tag title and add the site name (no pipe characters)
       
$vars['head_title'] = $tags['title'] .' - '. $vars['site_name'];
      }
    }
  }
}
?>

The attached nodewords-title.zip contains all the code needed: the patch file, the already patched module file in case you can't do patches, the title.inc file in the correct folder (metatags), and the code you need to put into template.php.

Note: This is for nodewords-5.x-1.8-rc1.tar.gz, dated Jan 29, 2008.

Posted in:

cool mission to Drupal

woow, thats a cool hack for pimpin my drupal site. thanks for this awesome tutorial. THX

You have very great

You have very great knowledge having this subject.

Well said.

Well said.
Awesome, great tutorial . Thanks for sharing

Great work, keep it up.

Great work, keep it up. Excellent tutorial covering SEO basics, awesome.

what a useful tutorial,

what a useful tutorial, thank you

magnanimous-junior