2

Is it possible to add a meta content="noindex,nofollow" tag to the head on the page of a node created programmatically (and onl this node)? I have created the node like this:

function MODULENAME_install() {
  $bodytext = 'Some Text';

  $node = new stdClass(); 
  $node->type = "page"; 
  $node->title = "New title";
  $node->language = LANGUAGE_NONE; 
  node_object_prepare($node); 
  $node->body[$node->language][0]['value'] = $bodytext;
  $node->body[$node->language][0]['summary'] = '';
  $node->body[$node->language][0]['format'] = 'full_html'; 
  $node->path = array('alias' => 'new-title');
  node_save($node);
}
  • You can add a variable and save the node id with this field. Check the current node having same id as variable then add meta tag. – Sumit Madan Feb 4 '15 at 11:51
  • yeah but i guess that i would have to get the nid of the created node inside the install function, but the add meta tag should probably go in another function.. or am i wrong? and what function should i use to add the meta tag, and can i then use the nid from the install function in the new function. Im sorry but i am new at module development. – neessen Feb 4 '15 at 12:08
  • When you save the node the nid will be returned so based on the returned nid you could do pass this to a function that will add the meta tag. – Fons Vandamme Feb 4 '15 at 12:55
  • You can use hook_preprocess_html. – Sumit Madan Feb 4 '15 at 13:08
  • I have added this line after node save: $new_id = $node->nid; and then added this function but it does not work.. function MODULENAME_preprocess_html(){ if(arg(0) == "node" && is_numeric(arg(1)) && arg(1) == $new_id){ $noindex = array( '#tag' => 'meta', '#attributes' => array( 'name' => 'robots', 'content' => 'noindex', ) ); drupal_add_html_head($noindex, 'noindex'); } } – neessen Feb 4 '15 at 15:23
4

You can use below code for adding no-follow and no-index meta for the node. Put this code in your themes template.php file.

function MYTHEME_html_head_alter(&$head_elements) {
  if (arg(0) == 'node' && arg(1) == 'YOUR NODE NID' && !arg(2)) {
    $head_elements['MYMODULE_meta_robots'] = array(
      '#tag' => 'meta',
      '#type' => 'html_tag',
      '#attributes' => array(
        'name' => 'robots',
        'content' => 'noindex'
      )
    );
  }
}

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.