Add Meta Tags to Views

Update (May 17, 2013): The patch mentioned below has been committed, so this Views PHP solution is no longer necessary.


Add meta tags to views in Drupal by using the 'Views PHP' module and the drupal_add_html_head function.

Meta Tags is an excellent Drupal module for managing meta tags, however, it doesn't yet allow adding meta tags to views. There is an open issue on this topic: http://drupal.org/node/1804356. While code had been offered in the form of a patch, it still has some issues.

In the interim, Views PHP offers a solution for this issue by way of the drupal_add_html_head function.

My use case is to prevent search engines from indexing certain Views pages.

  1. Install and enable Views PHP.
  2. Add a 'Global: PHP' element to the View header.
  3. Insert the code below, customized for your use:
<?php
  $element
= array(
   
'#tag' => 'meta',
   
'#attributes' => array(
     
'name' => 'robots',
     
'content' => 'noindex',
    ),
  );
drupal_add_html_head($element, 'robots_noindex');
?>

Comments

I tried exactly this example. As long caching is disabled, this works fine ...

Add new comment