3

I would like to add a meta tag noindex from within the template.php file of my theme.

The purpose is to remove two nodes 404 error page from google's index, I would like to do it like so :

// in mytheme/template.php
function my_theme_preprocess_node(&$variables, $hook) {
   $node = $variables['node'] ;
   // '123' would be nid of the 404 presentation page
   if($node->nid == '123'){
   /* META NO INDEX CODE GOES HERE */

Is this possible to do in my theme, without using any module at all ?

3

You could use the following code.

function mytheme_preprocess_node(&$variables, $hook) {
  $node = $variables['node'] ;
  if ($node->nid == '123') {
    $element = array(
      '#tag' => 'meta', 
      '#attributes' => array(
        'name' => 'robots', 
        'content' => 'noindex',
      ),
    );
    drupal_add_html_head($element, 'mytheme_noindex');
  }
}

In Drupal 6, you just use drupal_set_html_head('<meta name="robots" content="noindex" />'), instead of drupal_add_html_head().

I would rather use the code in a module, as a theme should not know details such as the existence of a node with a specific ID. Plus, if the site has more than one theme enabled, or it is possible that a different theme is enabled in future, the code should be copied in each enabled themes, while a module will always be executed, independently from the theme being used.

  • Your snippet is exactly what I was looking for. Yes it's right I should use a module for this purpose, I guess I am just in a quick & dirty mood. – B2F Jun 22 '12 at 21:05

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.