Creating Custom Meta Tags with the Metatag Module

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.

Brian McMurray, Software Architect
#Drupal | Posted

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. name = My Metatags
  2. description = Provides my custom Metatags.
  3. core = 7.x
  4. version = 7.x-1.x
  5.  
  6. dependencies[] = metatag
  7.  
  8. files[] = my_metatags.metatag.inc

 

my_metatags.module

  1. <?php
  2.  
  3. /**
  4.  * Implements hook_ctools_plugin_api().
  5.  */
  6. function my_metatags_ctools_plugin_api($owner, $api) {
  7. if ($owner == 'metatag' && $api == 'metatag') {
  8. return array('version' => 1);
  9. }
  10. }

 

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. <?php
  2. //
  3. // Implements hook_metatag_info().
  4. //
  5. function my_metatags_metatag_info() {
  6. $info['groups']['my_metatags'] = array(
  7. 'label' => t('My Custom Metatags'),
  8. );
  9.  
  10. $info['tags']['my_custom_metatag'] = array(
  11. 'label' => t('My Custom Meta Tag'),
  12. 'description' => t('This is a custom meta tag'),
  13. 'class' => 'DrupalTextMetaTag',
  14. 'group' => 'my_metatags',
  15. );
  16.  
  17. return $info;
  18. }
  19.  
  20. //
  21. // Implements hook_metatag_config_default_alter().
  22. //
  23. function my_metatags_metatag_config_default_alter(array &$configs) {
  24. foreach ($configs as &$config) {
  25. switch ($config->instance) {
  26. case 'global':
  27. $config->config += array();
  28. break;
  29.  
  30. case 'global:frontpage':
  31. $config->config += array();
  32. break;
  33.  
  34. case 'node':
  35. $config->config += array(
  36. 'my_custom_metatag' => array('value' => 'This is a default value.'),
  37. );
  38. break;
  39.  
  40. case 'taxonomy_term':
  41. $config->config += array();
  42. break;
  43.  
  44. case 'user':
  45. $config->config += array( );
  46. break;
  47. }
  48. }
  49. }

 

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 McMurray

Software Architect