Adding robots meta tags to Drupal nodes having a given taxonomy term

,

Well, the title is a mouthful, but here’s the situation. You have some nodes that are classified with a certain taxonomy term and you want to tell Google not to index those pages, how do you do it? There is the excellent nodewords module that lets you manage all kinds of meta tags, including most use cases for the robots meta tag. You can assign meta robots for the taxonomy listing pages, but not for nodes that are tagged with a certain term. For that, you need a little scripting.

Fortunately, Drupal provides you with taxonomy_node_get_terms_by_vocabulary(). In Drupal 6, this takes the $node object and a vocabulary ID. Then you just have to cycle through the terms it returns and see if your term is in there.

In my case, I have some pages that need to be publicly accessible, but must not be in Google or other indexes. So I let those get tagged with a special taxonomy term (“custom” in my case, but it could be “noindex” or anything). All you need to know is the vocabulary ID and the term ID for the term you’re testing for and you can apply the meta noindex to any node. Sometimes node_load() carries a high cost, but since we’re only testing this on nodes and we’re testing for the node in question, that data should be cached and should not result in another database query.

function MYTHEME_preprocess_page(&$vars, $hook) {

// if this is a node and not being edited and it is tagged with term 41, then add a "noindex" tag
   if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
	    $node = node_load(arg(1));
	    $terms = taxonomy_node_get_terms_by_vocabulary($node, 2);
		
		foreach($terms as $term) {
		  if ($term->tid == '41') {
			$vars['head'] = drupal_set_html_head('<meta name="robots" content="noindex" />');
		    break;
		  }
		}
	}
}

In order to get the meta tag to show, you’ll need to clear your caches.

4 Responses to “Adding robots meta tags to Drupal nodes having a given taxonomy term”

  1. what about certain nodes? I want to restrict google bot index some nodes which are produced by certain content type is this possible ?

  2. I don’t think this is possible out of the box, but you have a couple of options. If nodes of a certain content type have the same path for the first part and a unique path component, you could do it from the Custom Meta Tags settings

    [YOURSITE.TLD]/admin/content/nodewords/custom/add

    Otherwise, you could do the same as what I did above up through the nodeload() call and then just test for node type… so perhaps somethign like this

    function MYTHEME_preprocess_page(&$vars, $hook) {

    if (arg(0) == ‘node’ && is_numeric(arg(1)) && is_null(arg(2))) {
    $node = node_load(arg(1));
    if ($node->type == “myType”){
    $vars[‘head’] = drupal_set_html_head(‘[your meta tag here]’);
    }
    }
    }

    That’s just off the cuff – totally untested. Let me know if it works.

  3. Ok pal, I will give a try and let you know
    I appreciate for your help!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>