Boost your website traffic with one simple piece of code

Joris Snoek | 25/02/2010 - 18:14

Ever wanted to boost your site traffic with not that much effort?

Well, after reading The 7 Harsh Realities of SEO I came up with an idea: a very fast way to implement this in Drupal: "All I did was make the post title the title tag and the first sentence of each blog post the meta description."

As I was reading this article, I thought: "This shouldn't be that hard in Drupal". Without making a module but with a quick fix, here is howto:

Add the following code in your theme's template.php

  1. /**
  2.  * Override or insert PHPTemplate variables into the templates.
  3.  */
  4. function YOURTHEME_preprocess_page(&$variables) {
  5. //Get taxonomy tags from current node.
  6. foreach ($variables['node']->taxonomy as $tid => $tag){
  7. $tags .= ', ' .$tag->name;
  8.  
  9. //Strip body text; strip tags & and set max length
  10. $varText = check_plain(strip_tags($variables['node']->body));
  11. $description = check_plain(substr($varText, 0, 154));
  12. $tagshead = check_plain($tags);
  13.  
  14. //Fill the $head variable
  15. //Edit 'Lucius' to own company or something
  16. $variables['head'] = drupal_set_html_head(
  17. '<meta name="keywords" content="Lucius, '. $tagshead.'" />
  18. <meta name="description" content="'.$description.'" />
  19. <meta name="title" content="'.drupal_get_title().'" />
  20. );
  21. }
  22. }

This code will:

  • Add your tags to keywords meta-tag
  • Add your page title to title meta-tag
  • Add first 154 characters of your body-text to description meta-tag

NOTE
This is a very quick fix, it doesn't work with for example the module nodewords. To do it clean, you have to produce a module and test around with other Drupal seo modules.
Maybe, if we have some time left here at Lucius HQ, we'll provide a module. Hit me on Twitter if you'd like that! And maybe there allready is a module that provides this light weighted, nodewords partly allready does the trick. Please let me know on Twitter. Comments should also be here soon :-)

Related links
Optimize a Single Post On Your Blog for SEO
Kristen.org -- Drupal seo module list

Over Joris Snoek

Joris Snoek is Full time Drupal verslaafd, Project leider bij Lucius & Technology-freak. Tevens afgestudeerd in bedrijfskundige informatica en houdt veel van: muziek productie, snowboarden, tennis & fitness. En: heeft een zwak voor Duitse herders.

Over Peter Terpstra

Peter Terpstra is accountmanager bij Lucius.

Relatiebeheer en trajectbegeleiding van begin tot eind vallen onder zijn hoede.

Laatste tweet van Joris

joris_lucius: Looking into #mongodb #drupal implementation. Looks promising so far. #performance http://drupal.org/project/mongodb

Volg alle tweets van Joris

Zoeken