Adding Meta tags in Drupal node, simplified!!


I thought of writing this while working for one of my drupal project complainsindia.com. This is my first project in drupal so this may not be the best way to do it but this is one with most control on your meta tags i feel.

I have tried modules like meta tags, meta tags quick and page title to achieve similar functionality but all of them feel bugy and limited in what they can do. For example while "meta tag quick" allowed to add path based meta tags but not in forum nodes, Page title allow titles but only with patterns, not custom ones. And all of this drew me nuts figuring out.

So i decided to hack the drupal theme and do it myself. I found the help from many blogs, but not a complete solution. So now when i am done i feel it would be helpful for others if they need similar solutions.

So here is what we gonna do :
1- use the hook <theme-name>_preprocess_html() of drupal to pre-process page before it is gong to be displayed.
2- identify the pages to display the meta tags.
3- Finally add all needed meta tags on the page.

Step 1:

Open template.php file of your theme. if you are using a sub theme you can find it at sites/all/themes/<theme-name> or at theme/ folder if  you are using a core theme directly (not-recommended). if you do not find the file, copy from your base theme or just create one with same name.

find a function named <theme-name>_preprocess_html() inside this file. again create one if not able to find:

<?php
function <theme-name>_preprocess_html(&$variables) {}
?>

Step 2:

You will get the node ID of your current node at arg(1) array of the page. you can recognize your nodes with this unique ID. If you have enabled clean URL's and not able to see this ID then just print this argument
flush your cache and reload the page.

<?php
function <theme-name>_preprocess_html(&$variables) {
     print(arg(1)); }
?>

Now when you know the node ID of your node you can just create a switch condition for different nodes you wana add tags on,

<?php
function <theme-name>_preprocess_html(&$variables) {
     switch(arg(1)){
//current-issues, node id : 33
case "33" :
// Meta tag code here
break;

//node id 20
case "20" :
      // Meta tag code here
break;
     }}?>

you can also try using node_load() function here if you need more info about the node before putting the meta tags of your choice.

 $n = node_load(arg(1));

$type=$n->type;
$id=$n->nid;
.......///// etc.

Step 3:

Finally adding the meta tags is easy, To add new custom title just use this :
      $variables['head_title']= "New Title";

<?php
function <theme-name>_preprocess_html(&$variables) {
     switch(arg(1))
{
//node name-33
case "33" :
// title
                        $variables['head_title']= "New Title";
break;

//node name-20
case "20" :
      // title
                        $variables['head_title']= "New Title";
break;
      }}?>

Adding  keywords is easy too :

<?php
function <theme-name>_preprocess_html(&$variables) {
     switch(arg(1))
{
//current-issues, node id 33
case "33" :
// title
                        $variables['head_title']= "New Title";

                       //key-words
                        $page_keywords = array(
                             '#type' => 'html_tag',
                             '#tag' => 'meta',
                             '#attributes' => array(
                              'name' => 'keywords',
                              'content' => 'all keywords for this node',
                                )
                          );
                    drupal_add_html_head($page_keywords, 'page_keywords');
break;

//node id 20
case "20" :
      // title
                        $variables['head_title']= "New Title";
break;
      }}?>

we can add all other tags similarly, just change the name and content of tag you wanna add on above code. that's it.

Please suggest your views and improvements in the above code.


Comments

Unknown said…
Thank you! I did it for refresh meta tag for using with Drupal support module.
http://pastebin.com/WUHbCDrP