March 23, 2009

How to change a page meta description and keywords in Drupal 6

Filed under: Tips — Tags: , — Webopius @ 7:32 pm

In a recent article I wrote describing how to change and theme the format of the taxonomy pages in Drupal, FiNeX asked how to also get this title into your pages meta description. So, here’s how you can do it…

First, edit your theme

We will be creating two new variables called ‘$mysite_pagedescription’ and ‘$mysite_pagekeywords’ that you will be able to use in your theme templates.

Before creating these variables, add them to the pages that you want a custom meta description and meta keywords to appear on. These will usually be your theme’s page.tpl.php, page-front.tpl.php and other custom templates you’ve created.

Here’s an example of the new variables being used. Remember to place them in the <head> section of your template:


<meta name="description" content="<?php print $mysite_pagedescription ?>" />
<meta name="keywords" content="<?php print $mysite_pagekeywords ?>" />

Now, create the new Drupal variables

Having editing your theme template, just make sure everything is still working by viewing your site in a browser and checking the page source code. Until you create these new variables, your meta description and keywords fields should be empty.

To create and populate these variables, you need to create a theme pre-process function. If you are using the phptemplate engine, this function will need to be inserted into your theme’s template.php file (create this file in your theme’s directory if it does not already exist).

The pre-process function allows you to add/edit the theme’s variables before they are used in any of the template files. Here is an example of the function:

function phptemplate_preprocess(&$variables, $hook) {
    if ($hook == 'page') {
        // Generate page's description
        unset($variables['mysite_pagedescription']);
        unset($variables['mysite_pagekeywords']);
        $suffix1 ='Some words for the site that will appear after the dynamic description';
        $suffix2 = 'keyword1, keyword2, keyword3, keyword4 etc';
        if ($variables['is_front']) {
            $variables['mysite_pagedescription'] = $suffix1;
            $variables['mysite_pagekeywords'] = $suffix2;
        }
        else
        {
            if ($node->body) {
                $tease = node_teaser($node->body);
                $variables['mysite_pagedescription'] = check_plain(strip_tags($tease)) . ' ' . $suffix1;
                $variables['mysite_pagekeywords'] =  $suffix2;
            }
            // Figure out if we are viewing a Category (taxonomy) page...
            else if ($variables['template_files'][0] == 'page-taxonomy') {
                $term = taxonomy_get_term(arg(2));
                $d = check_plain(strip_tags($term->description));
                $variables['mysite_pagedescription'] = $d . '. ' . $suffix1;
                $variables['mysite_pagekeywords'] = $d . ',' . $suffix2;             }
            }
      }
}

Explanation of what phptemplate_preprocess() is doing

The preprocess function checks that it is preprocessing a page (rather than a node, comment or other content type).

Then, the default description and keywords are prepared ($suffix1 and $suffix2). There are three types of pages that this preprocess function is written for (you can add more if you need to):

1. The front page
2. A node view (single node or multiple nodes)
3. A taxonomy (category) view

On a Drupal front page, the preprocess function will simply use the default values.

In a Node view, the description will start with the node teaser followed by the default values. At this stage, you may want to create a dynamic custom set of keywords based on the node.

Finally, if the preprocess is being generated for a taxonomy page, the $d variable will be set to the taxonomy description which is then used at the front of both the meta description and keywords.

This, of course, is just one way to generate dynamic Drupal meta descriptions and keywords. You can tailor the example function above to your own specific site needs.

  • Tags