Output taxonomy terms/tags as meta keywords in Drupal 7

Estimated reading time of this article: 3 minutes 35 seconds

Do you have tags and you want to output them as meta keywords in HTML header?

There are several solutions. One solution is to use the Meta tags quick module with the patched Token module as described in issue Support for tokens in field values. But Token must currently be patched.

 

Another solution is to write a field formatter for taxonomy terms that also output the meta keywords. Put the following code in your module and choose in field display settings of your content type the 'Link generating meta keywords' format for your taxonomy term that you want to output.

Edit: The code below supports only one field. In the comments is a solution added which should support several fields. But I haven't tested this solution myself.

<?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;
}

/**
 * Adds 'Link generating meta keywords' and 'Plain text generating meta keywords'
 * field formatters for taxonomy reference terms.
 * Terms are extracted and they are added as meta keywords to the header.
 * Implements hook_field_formatter_info().
 */
function YOUR_MODULE_field_formatter_info() {
 
// taxonomy_field_formatter_info()
 
return array(
   
'YOUR_MODULE_taxonomy_term_reference_link' => array(
     
'label' => t('Link generating meta keywords'),
     
'description' => t('Adds additionally taxonomy terms as meta keywords to html head.'),
     
'field types' => array('taxonomy_term_reference'),
    ),
   
'YOUR_MODULE_taxonomy_term_reference_plain' => array(
     
'label' => t('Plain text generating meta keywords'),
     
'description' => t('Adds additionally taxonomy terms as meta keywords to html head.'),
     
'field types' => array('taxonomy_term_reference'),
    ),
  );
}

/**
 * Simply calls the base function taxonomy_field_formatter_prepare_view().
 * Implements hook_field_formatter_prepare_view().
 */
function YOUR_MODULE_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  return
taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
}

/**
 * Extracts taxonomy terms and adds them as meta keywords to the header.
 * Implements hook_field_formatter_view().
 */
function YOUR_MODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  switch (
$display['type']) {
    case
'YOUR_MODULE_taxonomy_term_reference_link':
     
// Reset to base taxonomy formatter name
     
$display['type'] = 'taxonomy_term_reference_link';
     
YOUR_MODULE_taxonomy_terms_add_head($items);
      break;
    case
'YOUR_MODULE_taxonomy_term_reference_plain':
     
// Reset to base taxonomy formatter name
     
$display['type'] = 'taxonomy_term_reference_plain';
     
YOUR_MODULE_taxonomy_terms_add_head($items);
      break;
  }

 
// Call base function
 
return taxonomy_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
}

/**
 * Sets meta keywords to the header using the terms of the $items array.
 * Does not set keywords for front page.
 * @return
 *  Returns FALSE in case of front page, otherwise TRUE.
 */
function YOUR_MODULE_taxonomy_terms_add_head($items) {
  if (!
drupal_is_front_page()) {
   
$terms = array();
    foreach (
$items as $delta => $item) {
     
$name = ($item['tid'] != 'autocreate' ? $item['taxonomy_term']->name : $item['name']);
     
$terms[] = check_plain($name);
    }

   
_YOUR_MODULE_add_head(array(
     
'name' => 'keywords',
     
'content' => implode(', ', $terms),
    ));
    return
TRUE;
  }
  return
FALSE;
}
?>