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










6 comments
4th Mar, 10
shouldn't it be arg(0) and arg(1) ?
http://api.drupal.org/arg
Josh
4th Mar, 10
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!!
31st May, 10
Thanks for sharing information on seo meta tags. Great effort you have done.
7th Oct, 10
Very interesting approach ..
I'm still learning how to create hook, gradually get there =)
21st Jan, 11
Is there any way to utilize this API to change the canonical link of a module-created page?
17th Mar, 11
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