First at all, I know that this isn't the correct way to do this in D7, but I need to do in this way.

I've included a file in node.tpl.php:

include DRUPAL_ROOT . '/sites/all/themes/mytheme/inc/node_functions.php';

In this included file I have a function which, afterwards I want to call from a node's content-body.

Currently, if I call the function from a node content, I get a WSOD. I also checked if within a node-content the function exists, with a PHP function:


but it returns FALSE, so the function no exists in this 'scope'.

In summary, I want a collection of helpful functions included/attached to a node, but I don't want to write the include/require statement everytime I'm creating a node. I only want to simple 'call' those functions. So, how can I do it?

Thanks in advance.

  • Do it in preprocessors in template.php. As you say you know that that is the right way. – Queenvictoria Nov 29 '15 at 21:57

Implementation Issues

  1. Included a file in node.tpl.php
  2. I have a function which, afterwards I want to call from a node's content-body

Sorry but I think these are not following best practices. As you can see If these was possible then it would have worked because it's just including a PHP file and calling a function in it but I think Drupal doesn't do it that way and runs the PHP in the body field way before node.tpl.php w/c results to no included PHP file at that time yet.

Suggested solution

Might I suggest, it's better for us to:

  1. Create a custom module
  2. Add your helper functions there with any structure of the files you can but following Drupal standards.
  3. Use Drupal hooks if needed because if you place your functions in the .module file, it will be available already for use.

One example of this way is after adding your helper functions into the .module file and want to execute/run functions when viewing the node, you can use hook_node_view. Maybe add some scripts, remove data, alter data and etc. and you can also do conditional statements there if you want to run specific things for a specific content type.

This just doesn't align your site with best practices but makes it easier to maintain in the long run with less technical debt.

Hope this helps you to be able to call/execute your needed functions on nodes.


You can remove the PHP file inside your theme and transfer the functions/codes inside it in the .module file. After that, you need to Clear All Caches then that will make your functions available or callable.

You don't need to use the include_once or hook function.

  • Ok @Leolando, I created my first custom module called 'render image'. So, in my .info, I wrote: name = Image Render description = Provides a user API for render images in a custom way. core = 7.x And, in my .module file: function image_render_node_view(&$node, $view_mode) { include_once DRUPAL_ROOT. '/sites/all/themes/my_theme/inc/node_functions.php'; ` }` But It still doesn't work. As I'm newby creating drupal modules and sincerelly, the info for do that in drupal.org is a bit confussing to me, could you write an example, please? – donbuche Nov 30 '15 at 11:00
  • @donbuche no problem at all! I have updated my answer. Basically you wont need the inc/node_functions.php file anymore because you will need to transfer the functions in there into the YOUR_MODULE.module file. Anything in there will be made available in the site. – Leolando Tan Nov 30 '15 at 20:50
  • Hi @Leolando Tan, I finally could reach my goal. Your comment: "You don't need to use the include_once or hook function", advising me that it's not required to write a function associated to a hook was so easy to understand to me. In other words, a module with a generalist functions works like a 'global include' so I can afterwards call and use them whenever I want to do it. Thanks very much for your help. – donbuche Dec 1 '15 at 14:29
  • Yes you are right! You're very welcome and glad you're exploring Drupal! :) – Leolando Tan Dec 1 '15 at 18:36

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.