how do I put meta tags on my index page?

Comments

drumm’s picture

First look through the list of modules to make sure it doesn't already exist. If not then the best thing to do is make a module. This could be done by editing your theme, but since this doesn't affect the look of the page (I would consider meta tags to be content) then it really doesn't belong there.

First, get started on your module by implementing some required hooks like _help. If you want to make the meta tag contents configurable via a web interface implment the _settings hook.

Now we want to actually put stuff in the header. The _init hook is a good place to do this sinc eit will be executed once for each page view. Just call drupal_set_html_head('meta tags go here') to put meta tags or anything else in the html head element. For only the front page use an if statment like: if ($_GET['q'] == variable_get('site_frontpage', 'node'))

If you did implement the _settings hook then you can access those settings the same way you did to prefill that page with the form values, using variable_get.

If you think you made something worthy of being seen by others then save it in the contributions CVS repository.

frjo’s picture

There was a keywords.module for older Drupal version and I have hacked it to work with 4.4.x. It seems to work for me at least.


function keywords_help($section) {
  $output = '';
  switch ($section) {
    case 'admin/help#keywords':
    case 'admin/system/modules/keywords':
      $output = "Search engines often use these <a href=\"http://www.w3.org/TR/1998/REC-html40-19980424/struct/global.html#h-7.4.4\">META</a> keywords to determine what topics are discussed at your site. The keywords that you choose do affect their relevance rankings, so some consideration to these keywords is wise.<br /><br />Tags beyond the META tag are also allowed here. In fact, this is a good place to add any tags like script, object, link, and others which are <a href=\"http://www.w3.org/TR/1998/REC-html40-19980424/struct/global.html#h-7.4.1\">allowed in the HEAD section of a web page</a>.";
      break;
    case 'admin/system/modules#description':
      $output .= t("Manage the meta tags outputted by drupal web pages.");
      break;
  }
  return $output;
}  

function keywords_perm() {
  return array("administer keywords");
}

function keywords_settings() {
  $output .= form_textarea(t("Keywords"), "keywords_meta", variable_get("keywords_meta", ""), 90, 6, t("The Meta tags which should be inserted into all web pages."));
  return $output;
}

function keywords_init() {
  if (variable_get("keywords_meta", "")) {
    drupal_set_html_head(variable_get("keywords_meta", ""));
  }
}  

Gaurav Chaturvedi’s picture

Where do I paste that exactly? I don't know php very well...do I create a new block? And then enable it..but will that auto do it for the main page?

frjo’s picture

Save the code to a file with the name "keywords.module". Put in in the modules folder where all you other modules are. Activate and configure it at admin/system/modules.

pss0ft’s picture

Hi,

I get the feeling that the whole meta instruction has to be put in. Not only the keywords? Right?

If so. Is it not possible to adjust this one so only the keywords has to be put in separated by ,?

It would be nice...

Grtz. Henk

SupaDucta’s picture

I believe this is actually wider and better solution, allowing to input any HEAD tag without having to add to set_drupal_head. Might be very useful.

SupaDucta’s picture

Frjo, can you give me a hand with your recoding of the keyword module?

I am using a keyword module and in general it works correctly.

However I am experiencing a small problem with users not logged in - cached pages output.

The error that unlogged visitors are displayed is:

Fatal error: Call to undefined function: drupal_set_html_head() in /home/beauty/public_html/modules/keywords.module on line 27

If I log in, the error dissapears. If I log out, refresh and it appears again, even though for example cron.php was run a minute ago.

From what I see, cron is called neatly via crontab in cPanel and it works.

Now the function in keyword module is:

 function keywords_init() { 
   if (variable_get("keywords_meta", "")) {
    drupal_set_html_head(variable_get("keywords_meta", ""));
 }
} 

How can this be fixed? This never occurs to users logged in, only to users not logged in - I presume it has something to do with page caching.

frjo’s picture

I don't know enough about Drupal yet to fix this bug. I just saw it on the site I use the keywords.module on also, it was at the bottom of the page and only for anonymous users.

Lets hope some other users can give us a hint.

SupaDucta’s picture

Well, I've come up with a solution. Maybe not the ideal one, but works great.

Basically the last function in keywords.module you have moddded goes out, leaving the complete module code as follows:

 function keywords_help($section) { 
  $output = ''; 
  switch ($section) { 
    case 'admin/help#keywords': 
    case 'admin/system/modules/keywords': 
      $output = "Search engines often use these <a href=\"http://www.w3.org/TR/1998/REC-html40-19980424/struct/global.html#h-7.4.4\">META</a> keywords to determine what topics are discussed at your site. The keywords that you choose do affect their relevance rankings, so some consideration to these keywords is wise.<br /><br />Tags beyond the META tag are also allowed here. In fact, this is a good place to add any tags like script, object, link, and others which are <a href=\"http://www.w3.org/TR/1998/REC-html40-19980424/struct/global.html#h-7.4.1\">allowed in the HEAD section of a web page</a>."; 
      break; 
    case 'admin/system/modules#description': 
      $output .= t("Manage the meta tags outputted by drupal web pages."); 
      break; 
  } 
  return $output; 
}   

function keywords_perm() { 
  return array("administer keywords"); 
} 

function keywords_settings() { 
  $output .= form_textarea(t("Keywords"), "keywords_meta", variable_get("keywords_meta", ""), 90, 6, t("The Meta tags which should be inserted into all web pages.")); 
  return $output; 
} 

In common.inc, function drupal_get_html_head() gets another line added:

$output .= variable_get("keywords_meta", ""). "\n";

of course, before

return $output . drupal_set_html_head();

And the module is fully operational. Yooooohoooooo! Stewardess, drinks for the boys! :tongue:

Thank you for this module, I really need it's features of adding META tags. :)

mercury66’s picture

When I install this module I get:

Parse error: parse error in /../drupal/modules/keywords.module on line 3

What have I done wrong?

Cheers,
E

Bèr Kessels’s picture

Could you please use the drupal issue tracker, where possible? That way it is much easier for a developer to pick up the cahnges, check them and place them in a new release.

With kind regards, and thanks for the work,

[Ber | webschuur.com]