Skip to main content

Creating Custom Meta Tags with the Metatag Module

Brian McMurray | Software Architect

August 29, 2012


Meta tags are one way that content authors can add extra information to a webpage, typically for the benefit of machines (like search engines) to learn more about the purpose and meaning of a webpage. You may recall that once upon a time it was a “search engine optimization” technique to fill the “keywords” meta tag with long lists of words to try to bump up your placement in search sites like Google. The “keywords” meta tag won’t help you much in Google anymore, but that doesn’t mean that meta tags have no use anymore. Perhaps you’d like to provide Open Graph for Facebook, or perhaps you have your own custom set of meta tags for use in an enterprise Google Search Appliance or other tool. The Meta Tags module is your answer in Drupal 7 for adding these meta tags to your website and being able to customize them for individual pages. The Meta Tags module provides some of the traditional meta tags like “keywords” and “description” out-of-the-box, and has some plugins for Open Graph, and also has a fairly simple API for integrating your own custom meta tags. To declare your own custom meta tags, you need to declare them in a custom module. To get started, create your custom module directory my_metatags and create the following files: my_metatags.info

name = My Metatags
description = Provides my custom Metatags.
core = 7.x
version = 7.x-1.x

dependencies[] = metatag

files[] = my_metatags.metatag.inc

my_metatags.module

<?php

/**
 * Implements hook_ctools_plugin_api().
 */
function my_metatags_ctools_plugin_api($owner, $api) {
  if ($owner == 'metatag' && $api == 'metatag') {
    return array('version' => 1);
  }
}

What we’ve done here is to create a new custom module called my_metatags and we’ve declared in the .info file that we will be including a file called my_metatags.metatag.inc. In my_metatags.module we’ve implemented hook_ctools_plugin_api to tell CTools where to find our metatag plugin. Now we need to create my_metatags.metatag.inc:

<?php
//
// Implements hook_metatag_info().
//
function my_metatags_metatag_info() {
  $info['groups']['my_metatags'] = array(
    'label' => t('My Custom Metatags'),
  );

  $info['tags']['my_custom_metatag'] = array(
    'label' => t('My Custom Meta Tag'),
    'description' => t('This is a custom meta tag'),
    'class' => 'DrupalTextMetaTag',
    'group' => 'my_metatags',
  );

  return $info;
}

//
// Implements hook_metatag_config_default_alter().
//
function my_metatags_metatag_config_default_alter(array &$configs) {
  foreach ($configs as &$config) {
    switch ($config->instance) {
      case 'global':
        $config->config += array();
        break;

      case 'global:frontpage':
        $config->config += array();
        break;

      case 'node':
        $config->config += array(
          'my_custom_metatag' => array('value' => 'This is a default value.'),
        );
        break;

      case 'taxonomy_term':
        $config->config += array();
        break;

      case 'user':
        $config->config += array( );
        break;
    }
  }
}

In this file we are implementing two hooks provided by Meta Tags, hook_metatag_info() and hook_metatag_config_default_alter(). The code in hook_metatag_info() does two things: 1) Creates a new Meta Tags group called “My Custom Metatags” and declares a single custom metatag “my_custom_metatag.” By default, this metatag will get output on a page like:

<meta name="my_custom_metatag" content="This is a default value." />

The code in hook_metatag_config_default_alter() provides default values for our custom meta tag. The defaults can of course be overriden within the Meta Tags administration area and additionally on a per-entity basis (node, taxonomy term, etc) based upon your configuration of the module.


Recommended Next
Development
A Developer's Guide For Contributing To Drupal
Black pixels on a grey background
Development
3 Steps to a Smooth Salesforce Integration
Black pixels on a grey background
Development
Drupal 8 End of Life: What You Need To Know
Woman working on a laptop
Jump back to top