3

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?

1 Answer 1

3

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.

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.