Including files in template.php. failed in some cases (when using some contrib modules) In template.php, in theme__preprocess_page function, I am including files (as my template.php grew big, I decided to separate some operations for readability) like this:

 function THEME_preprocess_page(&amp;$variables) {   
// .. skipped non-related code
 include  $_SERVER['DOCUMENT_ROOT'] . "/" . path_to_theme() . "/template_includes/file1.inc";   

}


But sometimes some contrib modules complain about this. Currently I have an issue where Print module gives the following message:

Warning: include(): Failed opening 
'/var/www/drupal/sites/all/modules/contrib/print/template_includes/file1.inc' **for inclusion** (include_path='.:/usr/share/php:/usr/share/pear') **in THEME_preprocess_page() (line 80 in** /var/www/drupal/sites/all/themes/THEME/template.php).


What causes paths mess?
If you read the docs for path_to_theme(), there is this tidbit:


  It can point to the active theme or the module handling a themed implementation. For example, when invoked within the scope of a theming call it will depend on where the theming function is handled. If implemented from a module, it will point to the module. If implemented from the active theme, it will point to the active theme. When called outside the scope of a theming call, it will always point to the active theme.


The Print module invokes preprocess functions within its own preprocesss functions (look at the code), so I think that is why yours is getting called with a scope other than your theme.

You should really do your include as

include drupal_get_path('theme', 'YOURTHEME') . '/template_includes/file1.inc';


or

include dirname(__FILE__) . '/template_includes/file1.inc';


There is debate in the Drupal community which is best.

Personally, I think including code within a function is a really bad practice, and would recommend splitting out whole functions into separate file(s), and doing the include at the top.  If you go that route, then you using the second form is best.