4

If a node has a term reference to a specific term (say termid 123) I want to add this META tag to prevent search engine spiders from indexing the page:

<meta name="robots" content="noindex">

Is hook_page_build() the right hook for this?

4

I think you definitely could use hook_page_build() (it would work), but since that hook is really intended for use with page.tpl.php, and meta tags are outputted in html.tpl.php, I think it might not be the 'Drupal' way.

Another option might be to use hook_html_head_alter(), which allows you to alter the <head> tags before they're rendered:

function MYMODULE_html_head_alter(&$head_elements) {
  if ($node = menu_get_object()) {
    $tid = function_to_get_term_id();
    $related_tids = field_get_items('node', $node, 'field_FIELD_NAME');

    if (!empty($related_tids) && $related_tids[0]['tid'] == $tid) {
      $head_elements['MYMODULE_meta_robots'] = array(
        '#tag' => 'meta',
        '#type' => 'html_tag',
        '#attributes' => array(
          'name' => 'robots',
          'content' => 'noindex'
        )
      );
    }
  }
}

You might also want to look at adding rel="nofollow" attributes on the links to those node in teaser displays, it might help speed up the process a bit.

  • thanks Clive, that's awesome! I like the rel="nofollow" ideas as well!!! – uwe May 10 '12 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.