Preprocess function for node.tpl.php file I've watched a million videos and read a million tutorials trying to figure out how to create a new variable for use in a version of a node.tpl.php file by utilizing preprocessing functions, but can't figure out what I'm doing wrong.

First, I copied the node.tpl.php file, renamed it node--nameofnodetype.tpl.php, and saved it in my theme folder. Then, I edited the template.php file, trying different variations of the following code.

function theme_preprocess_nodename(&amp;$variables, $hook) {
  $variables['link_name'] = 'link content';
}


I flushed the cache, and when I tried printing the new variable in the new node file, I got nothing. Can anyone tell me what I'm doing wrong?
To create a variable for any type of node you should use yourtheme_preprocess_node() function, and in this fuction you should specify a type of node.

Example:



/**
 * Override or insert variables into the node templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("node" in this case.)
 */

function YOURTHEME_preprocess_node(&amp;$variables, $hook) {

  //$variables['sample_variable'] = t('Lorem ipsum.');
  // Optionally, run node-type-specific preprocess functions, like
  // YOURTHEME_preprocess_node_page() or YOURTHEME_preprocess_node_story().
  //$function = __FUNCTION__ . '_' . $variables['node']-&gt;type;
  //if (function_exists($function)) {
  //  $function($variables, $hook);
  //}

  // define $node object, so it's be easier to use it
  $node = $variables['node'];

  // add variables to node--nameofnodetype.tpl.php file
  if ($node-&gt;type == 'nameofnodetype'){

      // $variables: an array with variables
      // you need to add one, so just define it
      $variables['my_variable'] = 'whatever';

      // also you can rewrite those variables which already exists
      // for example variable: $title
      $variables['title'] = t('The new title');
  }
}


In your node--nameofnodetype.tpl.php file:

// check if your new variable exist
print $my_variable;


 * Don't forget replace 'YOURTHEME' to your theme name