Output meta description and keywords for selected paths with Drupal 7

Estimated reading time of this article: 3 minutes

You want to output meta keywords for selected pages like the front page. One solution is to use Meta tags or the Meta tags quick module. They do the job. But if you have only a few pages and you want to avoid the overhead of a module with DB accesses, you can use a simple hook.

Write in your module the following code.

<?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');
 
switch ($path) {
    case
'node':
     
_YOUR_MODULE_add_head(array(
       
'name' => 'description',
       
'content' =>  'YOUR DESCRIPTON',
      ));
     
_YOUR_MODULE_add_head(array(
       
'name' => 'keywords',
       
'content' => 'YOUR KEYWORDS',
      ));
    break;

   
// Nodes
   
case 'node/1':
     
_YOUR_MODULE_add_head(array(
       
'name' => 'description',
       
'content' => 'YOUR DESCRIPTON',
      ));
     
_YOUR_MODULE_add_head(array(
       
'name' => 'keywords',
       
'content' => 'YOUR KEYWORDS',
      ));
    break;

   
// Views
   
case 'path/to/view':
     
_YOUR_MODULE_add_head(array(
       
'name' => 'description',
       
'content' => 'YOUR_DESCRIPTON',
      ));
     
_YOUR_MODULE_add_head(array(
       
'name' => 'keywords',
       
'content' => 'YOUR KEYWORDS',
      ));
    break;
  }

 
// If no description is set, set the general description.
 
_YOUR_MODULE_add_head(array(
   
'name' => 'description',
   
'content' => 'Your general meta description.',
  ));

}

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

Instead of putting the code in a module, it could also go in your template.php.

<?php
**
 * Implements
template_process_page().
 */
function
YOUR_THEME_process_page(&$variables) {
 
// Adds meta description and meta keywords to the header in a simple
  // hard coded fashion.
 
$path = $_GET['q'];
 
// dpm($path);
 
switch ($path) {
    case
'node':
     
_YOUR_MODULE_add_head(array(
       
'name' => 'description',
       
'content' => 'YOUR DESCRIPTION',
      ));
     
_YOUR_MODULE_add_head(array(
       
'name' => 'keywords',
       
'content' => 'YOUR KEYWORDS',
      ));
    break;
  }

 
// If no description is set, set the general description.
 
_YOUR_MODULE_add_head(array(
   
'name' => 'description',
   
'content' => 'Your general meta description.',
  ));

}
?>