On my website, I have a number of view that are all displayed in the same way, but the content might be described in several different ways. I have looked at the Meta Tags module, nodewords by path, nodewords by type, integrated metatags... And they all don't exactly do what I'm looking for.

The issue is this, All of these seem to be static definitions based on conditions, for instance the type of content (View? doesn't make sense), or the path (The path may have hundreds of variants, but will several things in common. For instance 'location/[1]' should have the keyword [1], and a description such as "Companies in [1]" and "location/[1]/[2]" should have both keywords [1], and [2], and a description such as "Companies in [1] providing [2] service".

I guess the easiest way to address an issue like this (since I set the title dynamically in PHP) would be do set the keywords and description dynamically in PHP, however, I can't seem to find a way to do this.

Comments

matkeane’s picture

Hi,

Well, if you can work out the logic for your PHP code, then you could add your custom code to your theme's _preprocess_page function. I did this recently on a site to pull the keywords from both the node title and its taxonomy terms, and add the HTML output to a new page variable - $vars['meta_tags'] - which I then output in the theme's page.tpl.php.

I didn't spend too much time on it, as I think the keywords and description tags are much less important for SEO than the page title tag and h1 header, but I figured it wouldn't hurt and could improve the appearance in search results.

Hope that helps...

summit’s picture

Hi Matthew,

Could you post your code here please? For some custom pages want to do the same.
Thanks a lot in advance!
greetings,
Martijn

matkeane’s picture

Some of this is a bit hacky - I'm sure something like the node words module does things in a smarter way, but if you're looking for a quick solution, this might help.

// This snippet goes in the theme's _preprocess_page function.

/* get site mission on all pages */
  $vars['slogan'] = variable_get('site_slogan', 'ABC is the leading UK organisation in the field of this and that');
  
/* Build header meta tags */
  $meta_tags = array();

// Use slogan for description
  $meta_tags['description'] = array('type' => 'name');
  $meta_tags['description']['content'] = $vars['site_name'] .' (ABC) - '. $vars['slogan'] .' Based in London, UK.';
  
// Build keywords from node title
  $meta_tags['keywords'] = array('type' => 'name');
  $meta_tags['keywords']['words'] = array('this','that','the-other','miscellaneous','keywords','always','applicable');
  $node_title_words = explode(" ", $vars['title']); // get possible words from node title
  $keywords_ignore = array('and','in','using','is','if','the','by'); // words to be ignored
  foreach($node_title_words as $title_word) {
    $title_word = strtolower($title_word);
    // Check that word isn't in ignore list, or already in list of default keywords
    if(!in_array($title_word, $keywords_ignore) && !in_array($title_word, $meta_tags['keywords']['words'])) {
      $meta_tags['keywords']['words'][] = $title_word;
    }
  }
  // concatenate word list with commas
  $meta_tags['keywords']['content'] = join(', ', $meta_tags['keywords']['words']);

// Hide horrible MS Image toolbar thing
  $meta_tags['imagetoolbar'] = array('type' => 'http-equiv');
  $meta_tags['imagetoolbar']['content'] = 'no';

// Build HTML output
  $meta_tags_output = '';
  foreach($meta_tags as $tag_name => $tag_data) {
    $meta_tags_output .= '<meta '. $tag_data['type']. '="'. $tag_name .'" content="'. $tag_data['content'] .'" />'."\n" ;
  }
  $vars['meta_tags'] = $meta_tags_output; 

The 'description' just uses the site slogan, and the 'keywords' uses some default terms plus words found in the page title (which is usually inherited from a node).

I've tried to remove some of the specifics related to the site I use this for, but still keep things comprehensible. Basically, I build an array of meta tags which are then compiled into a page variable which can be printed in the page.tpl.php:

[...]
<meta http-equiv="content-language" content="<?php print $language->language ?>" />
<?php print $meta; ?>
<?php print $meta_tags; ?>
<?php print $head; ?>
<?php print $styles; ?>
[...]

There should probably be some more checking in there for duplicate words, but hopefully it should give you some ideas...

matkeane’s picture

Just to flesh things out a bit, since the original post talked about Views and Taxonomy, this is how I integrate them into the meta data in a simple way.

Again, in the _preprocess_page function, I have a bit of code that checks the template-file. Since Drupal and Views will suggest different template files for View pages and even specific taxonomy terms, we can differentiate different pages.

The original reason for doing this was that the client wanted more friendly page titles than just the taxonomy term. So, for example, instead of just 'British', the nationality vocab is modified so that the title becomes 'British Artists'. The updated title is then added to the list of keywords in the meta tag using the snippet in the previous post.

/* Check title / body classes on taxonomy pages */
  // Check for taxonomy views pages with a Term ID (tid) arg
  if($vars['template_files'][0] == 'page-taxonomy' && isset($vars['template_files'][2])) {
    $term_tid_parts = explode('-', $vars['template_files'][2]); // e.g: page-taxonomy-term-426
    if(is_numeric($term_tid_parts[3])) {
      $taxonomy_data = taxonomy_get_term($term_tid_parts[3]);
      if(isset($taxonomy_data->vid)) { // got vocab data
        // Modify page title according to Vocabulary ID (vid)
        switch($taxonomy_data->vid) {
          case 1: // nationality vocab
            $vars['title'] .= ' Artists';
            break;
          case 2: // regions/counties
            $vars['title'] = 'Locations in '. $vars['title'];
            break;
          case 3: // artwork materials
            $vars['title'] = 'Artworks using '. $vars['title'];
            break;
          case 4: // artwork type
            $vars['title'] = $vars['title'] .'s'; // add plural
            break;
          case 5: // Site section
            $vars['title'] = 'Pages in <em>'. $vars['title'] .'</em> section';
            break;           
        }
      }
    }
  }
anonymous07’s picture

Subscribe