Drupal 6: Define SEO Meta Tags dynamically using hook_nodewords_tags_alter

Submitted by Janak on Thu, 03/04/2010 - 10:32

Ever wanted to implement or alter meta tags on a custom page or a view? As most of you would have already heard of the excellent nodewords 6.x-1.11 (module which allows users to define their own meta tags for nodes and much more), we will use some of the very useful API functions of this module.

You can actually use the nodewords API to alter, inject and define your own meta data on virtually any page.

Say you have a view and you want to dynamically set the meta tags for that view? easy..

Step 1: Implement the hook_nodewords_tags_alter() hook

Write your own module or add to your existing custom module a new function like this. This is an implementation of hook_nodewords_tags_alter()

 
function mymodule_nodewords_tags_alter(&$tags, $parameters) {
  $args = explode('/', $_GET['q']);
  // define your own logic to make it url specific
  if($args[0] == 'foo' and $args[1] == 'bar'){ // we have a valid url
 
    //if you are using alternative logic to dynamically 
    //load up the meta data, implement that here
 
    //finally set the tags
    $tags['description'] = "this is my custom meta description";
    $tags['keywords'] = "this is my custom meta keywords";
  }
}

Note: if you are using an older version of Nodewords module (without the API), please check /blog/adding-meta-tags-taxonomyterm-override-view-pathauto-nodewords-and-views-46