Drupal 6: Define SEO Meta Tags dynamically using hook_nodewords_tags_alter

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 http://janaksingh.com/blog/adding-meta-tags-taxonomyterm-override-view-pathauto-nodewords-and-views-46


Bookmark and Share

6 comments

Anonymous's picture

shouldn't it be arg(0) and arg(1) ?

http://api.drupal.org/arg

Josh

Janak's picture

You are right Josh. 99% of the time arg() is all you need! I think the original code I posted above is slightly adapted as I was exploding on "-" using $_REQUEST['q']; But if arg() is all you are after, why not use the built in Drupal function!!

Anonymous's picture

Thanks for sharing information on seo meta tags. Great effort you have done.

Anonymous's picture

Very interesting approach ..

I'm still learning how to create hook, gradually get there =)

Anonymous's picture

Is there any way to utilize this API to change the canonical link of a module-created page?

Anonymous's picture

I'm glad to have strayed in to the post for the useful post on drupal 6 defining SEO meta tags using hooknodewordstagsalter.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apache>, <bash>, <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>, <vim>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options