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(&$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?

  • That preprocess function should be mytheme_preprocess_node() where mytheme is the name of your theme. Commented May 12, 2013 at 16:56
  • Yes, that's the way I wrote the preprocessing function, the same way as you describe. What else might I be doing wrong? Thanks.
    – Drupguy
    Commented May 12, 2013 at 17:01
  • 1
    If your theme includes a standard node.tpl.php, I'd try editing that to print the variable, just as a test. Also, it might help to install the Devel module so you can use the dsm() function to print the content of variables. Commented May 12, 2013 at 17:06
  • I might be wrong, but isn't that you need to have node.tpl.php present for node-[whatever].tpl.php to have effect? Commented May 12, 2013 at 22:49
  • I tried printing it in the nod.tpl.php file as well, but it's not printing anything. So it must be the code in the template.php file that's wrong. I took Jack-PL's advice below and tried his code, but it's not working. Ugh.
    – Drupguy
    Commented May 13, 2013 at 17:10

2 Answers 2


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.


 * 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(&$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']->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->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

  • Thanks, Jack-PL. I've tried what you've written above, but it's not working. I'm pretty new to Drupal, so please bear with me. I'm apt to use the code that's NOT commented out in your example. When you use terms like "$node" and 'node', do you literally mean I should write the term "$node", or are you using that as a place-holder? Also, by "$node->type" do you mean content type? Because I put the content type in there (which is the "nameofnodetype" in my hook above), but it didn't work. Then I tried putting both 'node' and 'content' in place of 'blog', but that didn't work either. Thoughts?
    – Drupguy
    Commented May 13, 2013 at 17:15
  • ok, but have you tried that your node--nameofnodetype.tpl.php file overrides the default node.tpl.php file?
    – Jack-PL
    Commented May 13, 2013 at 17:43
  • Yes, the node--nameofnodetype.tpl.php file definitely overrides the default node.tpl.php file.
    – Drupguy
    Commented May 13, 2013 at 18:08
  • I just noticed your addition to your original answer above, Jack-PL. Yes, I've already written "print $my_variable;" in the node--nameofnodetype.tpl.php file. But when I do that, it gives me an "undefined variable" error on my website. I've also tried writing "print render($content('my_variable')];" and "print render($my_variable)" which don't work. So I'm thinking I've written the preprocess function in template.php wrong.
    – Drupguy
    Commented May 13, 2013 at 19:02
  • you don't have to render your new variable. Just print it. Also you can print all available variables: <?php print '<pre>' . print_r($variables,1) . '</pre>'; ?> and try to find if the new variable exist there
    – Jack-PL
    Commented May 13, 2013 at 19:03

In Drupal 7, you'll need node--nameofnodetype.tpl.php

node__nameofnodetype.tpl.php is wrong (underscores define a system name)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.