Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Creating Custom Meta Tags with the Metatag Module

Parent Feed: 

Posted Aug 29, 2012 // 4 comments

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

1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
8
9
10
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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.

Brian is the foremost authority on all things Mobile at Phase2 Technology. Brian frequently speaks about topics like front-end web performance, jQuery Mobile, and RaphaelJS. He has been working with Drupal since 2005 and has presented at ...

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web