Generate automatic meta description for taxonomy term listings for Drupal 7

Estimated reading time of this article: 3 minutes

Search engines often print the meta description in the search result list. A good meta description might increase the click rate and is a good SEO measure.

An automatic meta description for taxonomy term listings is fairly easy in Drupal 7.

This article is related to the article Output meta description and keywords for selected paths with Drupal 7.

<?php
/**
 * Adds meta description and meta keywords to the header in a simple
 * hard coded fashion.
 * Implements hook_page_build().
 */
function YOUR_MODULE_page_build(&$page) {
 
$path = $_GET['q'];
 
// dpm($path, 'path');
  // dpm($page, 'page');
 
switch ($path) {
 
// Taxonomy terms
 
if (preg_match('|^taxonomy/term/(\d+)|', $path, $matches)) {
   
$tid = $matches[1];
   
$term = taxonomy_term_load($tid);
   
// dpm($term);
   
switch ($term->vocabulary_machine_name) {
      case
'tags':
      case
'keywords':
       
// Set description of term, if present, otherwise general description.
//         if ($term->description) {
//           _YOUR_MODULE_add_head(array(
//             'name' => 'description',
//             'content' => $term->description,
//           ));
//         } else
       
if ($term->vocabulary_machine_name == 'tags'){
         
_YOUR_MODULE_add_head(array(
           
'name' => 'description',
           
'content' => 'Listing of the articles of the tag "' . $term->name . '" of the website XY',
          ));
        } else if (
$term->vocabulary_machine_name == 'keywords'){
         
_YOUR_MODULE_add_head(array(
           
'name' => 'description',
           
'content' => 'Listing of the articles of the keyword "' . $term->name . '" of the website XY',
          ));
        }
       
_YOUR_MODULE_add_head(array(
         
'name' => 'keywords',
         
'content' => $term->name,
        ));
      break;
    }
  }
}
?>

The same helper method is used as in the previous article:

<?php
// Adds meta tag to internal storage that will be processed during page build.
// Copy of _metatags_quick_add_head($item = FALSE) of Meta tags quick module
// see module http://drupal.org/project/metatags_quick and code
// http://drupalcode.org/project/metatags_quick.git/blob/6b87f44670f79b6db43f03b605641487e65c8e07:/metatags_quick.module
function _YOUR_MODULE_add_head($item = FALSE) {
  static
$added_meta = array();
  static
$meta_data = array();
  if (!empty(
$added_meta[$item['name']])) {
    return;
  }
 
// Only output meta if content is not empty.
 
if ($item['content']) {
   
$content = $item['content'];
    if (!empty(
$item['type']) && !empty($item['entity'])) {
     
$content = token_replace($content, array(
       
$item['type'] => $item['entity'],
      ));
    }
    else {
     
$content = token_replace($content);
    }
   
// (Not nice) hack to separate multiple tags returned by token.
   
$content = preg_replace('/<\/a><a/', '<\/a>, <a', $content);
   
$content = trim(strip_tags($content));
   
$item['content'] = $content;
   
$meta_data[] = $item;
   
$element = array(
     
'#tag' => 'meta',
     
'#attributes' => array(
       
'name' => $item['name'],
       
'content' => $item['content'],
      ),
    );
   
drupal_add_html_head($element, 'metatags_quick_' . $item['name']);

  }
 
$added_meta[$item['name']] = TRUE;
}
?>